sed - append text to every line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed - append text to every line
# 1  
Old 02-01-2010
sed - append text to every line

Hi all

I tried this on an old version of sed on NCR Unix MP-RAS:

Code:
sed -e "s/$/nnn/" file1 >file2

This file (file1):
Code:
the cat sat on the mat.
the cat sat on the mat.

the cat sat on the mat.

becomes this (file2):
Code:
the cat sat on the mat.nnn
the cat sat on the mat.nnn
nnn
the cat sat on the mat.nnn

Now, I tried to port this sed script to GNU sed v.4.1.5 on RHEL5. Using the same script:

Code:
sed -e "s/$/nnn/" file1 >file2

my output looks like this:
Code:
nnn cat sat on the mat.
nnn cat sat on the mat.
nnn
nnn cat sat on the mat.

I tried awk instead of sed...
Code:
awk '{print $0 "nnn"} file1 >file2

but get the same results from both platforms (MP-RAS appends to end of line, RHEL overtypes first 3 chars of each line).


Any ideas why? Any ideas how to fix - I am running out of ideas.

TIA for any insight...

BRgds
JG
# 2  
Old 02-01-2010
Can it be you transferred the input file from a windows platform and forgot to convert to unix file format?
# 3  
Old 02-01-2010
sed - append text to every line

Hi Scrutinizer

It would appear not.

While it is true that the data has been copied from Windows, `cat'ing and `vi'ing the file shows up no ctrl-M characters as line ends.

Thanks for the input though, much appreciated.

BRgds
JG

---------- Post updated at 12:06 PM ---------- Previous update was at 11:59 AM ----------

Scrutinizer

Oops - my bad!

You were right. Even though VI / CAT showed no ctrl-m chars (they DO show up under MP-RAS when it occurs), the file was in fact encoded as DOS (x'0d0a' line ends). a quick dos2unix fixed it for me!

Thanks - I thought I was going nuts.

BRgds
JG
# 4  
Old 02-01-2010
It would explain the nnn at the beginning of the line, which can happen because of spurious carriage return characters. What happens if you create a new test file by hand with vi and then run your sed statement? Same thing? gsed should have no trouble with your script.

-- Our posts crossed --
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - Find a String and append a text end of the Line

Hi, I have a File, which have multiple rows. Like below 123456 Test1 FNAME JRW#$% PB MO Approver XXXXXX. YYYY 123457 Test2 FNAME JRW#$% PB MO Super XXXXXX. YYYY 123458 Test3 FNAME JRW#$% PB MO Approver XXXXXX. YYYY I want to search a line which contains PB MO Approver and append... (2 Replies)
Discussion started by: java2006
2 Replies

2. Shell Programming and Scripting

Use sed to append text to filenames if text not already present

I have some html with hrefs that contain local links to pdf filenames. These filenames should have standardised names, i.e. there should be a label prior to the ".pdf" filename suffix. There can be many of these links on a single line of text and some may already have the label. For example ... (13 Replies)
Discussion started by: adb
13 Replies

3. Shell Programming and Scripting

Search text and append using SED?

I have file . cat hello.txt Hello World I would like to append a string "Today " so the output is cat hello.txt Hello World Today I dont know which line number does the "Hello World" appears otherwise I could have used the Line number to search and append . (3 Replies)
Discussion started by: gubbu
3 Replies

4. Shell Programming and Scripting

sed append without using new line

im trying to append to the end of the line using sed but I want to do it without creating a new line the text to which I want to append is all in capital letters. I want to do something like this: LINE]Foo but when I do this: //a\ ] Foo it prints foo on a new line: LINE ]Foo ... (11 Replies)
Discussion started by: mrjavoman
11 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

append each line with text

hi, I've a file with some text in it, i need to append few strings in the beginning and end of each row. --in file richie matt .. --out file hi, 'richie' is here hi, 'matt' is here ... I tried with awk command, but it fails because of ' Thanks (2 Replies)
Discussion started by: dvah
2 Replies

8. Shell Programming and Scripting

How to append text to the second line of a file

Say I have a text file like: 1 3 4 How would I use ksh to put the number '2' into the second line of that file? I'm using OpenBSD so the sed syntax might be a bit different (I have no idea how to use sed, though) (4 Replies)
Discussion started by: guitarscn
4 Replies

9. Shell Programming and Scripting

How to append line with sed?

Input: gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-ugly Output should be: gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-ugly How can it be done with sed? (5 Replies)
Discussion started by: cola
5 Replies

10. UNIX for Dummies Questions & Answers

using sed to append text to the end of each line

Anyone know how to use SED to append a comma to the end of each line example: field1,field2,field3,field4 If i Cat /textfile ---- How can i append the end of /textfile with a comman? (8 Replies)
Discussion started by: Redg
8 Replies
Login or Register to Ask a Question