954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help with shell script

Hello everyone

i have a text file that contains something like this.

1 AC AT RR cOO
2 EE cFF HGB cEVY
3 WDU RWS cTY NBE


but it contains a lot of rows and that's an example of the data

what i want to do is to to create another text file using a shell script from this text file with these modifications:
1-let the same ordering of the same file
2-change each value that contains c to without c
3-add c to each value that does not contain c

e.g(1 AC AT RR cOO will become 1- cAC cAT cRR OO)


thanks in advance

weblover
Junior Poster
141 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

sed can do it. The trick is to deal with the 'c' so that sed doesn't remove it entirely until the new 'c' are added in. That's the point of the '%' in the first and last clauses of the script. I'm assuming that the leading digits in your example are line numbers and should never be changed. That allowed me to pay attention to leading white space in each token.

Here's my one liner:

sed -e 's/c\([^ ]*\)/%\1/g' -e 's/ \([^%][^ ]*\)/ c\1/g' -e 's/%//g' < $infile > %outfile
griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

thank you a lot for your answer , it worked

weblover
Junior Poster
141 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: