Add text at the end of line conditionally


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add text at the end of line conditionally
# 1  
Old 02-02-2011
Add text at the end of line conditionally

Hi All,

I have a file as below:

Code:
cat myfile

abcdef
NA
rwer
tyujkl
na

I wish to add the text ".txt" at the end of all lines except the lines starting with NA or na.

I know i can add text at the end of line using following command but I am not sure how to valiate the condition.

Code:
sed 's/$/\.txt/g' myfile >temp
mv temp myfile

Can any one help me on how to achieve this? Your help is highly appreciated.

Regards
Angshuman
# 2  
Old 02-02-2011
try
Code:
awk '{print ($0~/^NA|na/?$0:$0".txt")}' urfile

This User Gave Thanks to yinyuemi For This Post:
# 3  
Old 02-02-2011
Code:
awk '/^na|^NA/||$0=$0".txt"' file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 02-02-2011
Code:
sed '/^[Nn][Aa]$/{p;d;};s/.*/&.txt/' infile

This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 02-02-2011
Code:
sed '/^[Nn][Aa]/!s/$/.txt/' file

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-02-2011
@Scruti,
lol yes, it's better Smilie

what is suppposed to happen if the file contain a line like

Code:
Na, i am not a line like the others !

??
This User Gave Thanks to ctsgnb For This Post:
# 7  
Old 02-02-2011
Code:
 $  ruby -ne 'chomp; print ($_ !~ /^\s*(NA|na)\s*$/i ) ? $_+".txt\n": $_+"\n" ' file

This User Gave Thanks to kurumi For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add text to the end of line

Seems simple but ive been searching for a good hour of so I have a text file and would like to add a string to the end of line 5 ( as an example) to ake tings hard the line number we have to add the text to is stored in a variable cunningly name $Line_to_append any ideas on how this could... (2 Replies)
Discussion started by: dunryc
2 Replies

2. Shell Programming and Scripting

Conditionally add character at end of line

Hi, I would like have a shell script to check every line in a file to see if it ends with ";". If this is NOT the last character ";" should be added. MyFile.csv : web9331801;01/01/2014 23:39:35;;"93962";353150256; web9331802;01/01/2014 23:44:29;;"479288";353153538; web9331803;01/01/2014... (14 Replies)
Discussion started by: vg77
14 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Shell Programming and Scripting

trying to add text to beginning and end of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (0 Replies)
Discussion started by: pasc
0 Replies

5. Shell Programming and Scripting

find a certain line and append text to the end of the line

After I create printer queues in AIX, I have to append a filter file location within that printers custom file. within lets say test_queue.txt I need to find the row that starts with :699 and then I need to append on the end the string /usr/local/bin/k_portrait.sh. Now I've gotten the sed... (2 Replies)
Discussion started by: peachclift
2 Replies

6. Shell Programming and Scripting

Append text to end of every line

I've scoured the internet with mixed results. As an amateur I turn to the great minds here. I have a text file of 80 or so lines. I want to add ".pdf" to the end of each line. (For now that's it) Most of the internet points toward using "sed". I don't know coding but can figure things out... (4 Replies)
Discussion started by: spacebase
4 Replies

7. Shell Programming and Scripting

Get the 1st 99 characters and add new line feed at the end of the line

I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed). AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC MY * A02 RESALE 6990788000 Y... (3 Replies)
Discussion started by: udelalv
3 Replies

8. Shell Programming and Scripting

insert text at every end of the line

Hi, I just wanted to know if you have any idea or script to insert a text at everyend of the line, the text will vary. for example sample: this is line1 ok this is line2 ok this is line3 ok output: this is line1 ok /home/line1.txt this is line2 ok /home/line2.txt this is line3 ok... (6 Replies)
Discussion started by: invinzin21
6 Replies

9. Shell Programming and Scripting

add text to end of text file

Hi, I assume there is a simple solution, but as usual i can't find it! How can i add a line of text to the end of a text file on a new line? i.e file.txt ________________ this is my text file ________________ file.txt ________________ this is my text file WITH A NEW LINE... (6 Replies)
Discussion started by: leeRoberts2007
6 Replies

10. Shell Programming and Scripting

adding text to end of each line in a file

I'm needing to add a "hour:min" to the end of each line in a document. The document in this case is only going to be one line. if this inserts it at the end, what needs to be changed to add something at the end... /bin/echo "%s/^/$filler/g\nwq!" | ex -s $oFile Thank you... (2 Replies)
Discussion started by: cubs0729
2 Replies
Login or Register to Ask a Question