how to add text as the new first line without getting ^M?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to add text as the new first line without getting ^M?
# 1  
Old 02-10-2012
how to add text as the new first line without getting ^M?

hi all, first post here

how to add text as the new first line without getting the "^M"s in vim?

I've tries
Code:
echo "new_test" cat "file_to_be_edited" > new file

and
Code:
sed -i '1i\
new_test' file_to_be_edited

these two can successfully add new_test as the new first line, but both of them adds ^M to the end of each line, which i don't want that....

any solutions to this problem?

thanks ahead

Last edited by Franklin52; 02-11-2012 at 08:54 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 02-10-2012
Nothing in UNIX adds ^M unless you ask it to. Either you're editing these files in Windows, or you're running Cygwin or something like that. Cygwin is not UNIX. It retains all the usual Windows problems.

Putting two commands on one line doesn't make it two commands unless you separate them with ;

And if you want to group the output of those two commands together, you'd do so with ( )

so:

Code:
( echo "new first line" ; cat file ) > output

# 3  
Old 02-10-2012
Quote:
Originally Posted by Corona688
Nothing in UNIX adds ^M unless you ask it to. Either you're editing these files in Windows, or you're running Cygwin or something like that. Cygwin is not UNIX. It retains all the usual Windows problems.

Putting two commands on one line doesn't make it two commands unless you separate them with ;

And if you want to group the output of those two commands together, you'd do so with ( )

so:

Code:
( echo "new first line" ; cat file ) > output

thanks for your reply

i'm working on macos 10.7.3, and the server is linux which i don't the exact version.
i suppose i'm not even close to windows
# 4  
Old 02-10-2012
If you're getting carriage returns in it, I suspect one of the original files was edited in Microsoft Notepad before it was uploaded to these servers.
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

How to add the line to previous line in | delimited text?

Hi All, I am new to Unix and I have one challenge and below are the details. I have pipe delimited text file in that data has span into multiple lines instead of single line. Sample data. Data should be like below for entire file. 41|216|398555|77|provided complete NP outcome data ... (21 Replies)
Discussion started by: Narasimhasss
21 Replies

3. Shell Programming and Scripting

sed to add text in new line

help i need to add a "nfsd" in new line after cron ex: cron rpcbind output: cron nfsd rpcbind i use sed -e "/cron/G; s/$/nfsd/" myfile output: cron nfsd rpcbindnfsd (5 Replies)
Discussion started by: jamilzain
5 Replies

4. Windows & DOS: Issues & Discussions

Trying to add text to the beginning 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... (6 Replies)
Discussion started by: pasc
6 Replies

5. Shell Programming and Scripting

how to add text into the last line of text file

I need help with insert text to the last line of text file with echo command I know can do something like echo "i4\n$logtext\n.\nwq" | ex -s $file can insert to first line, but how can i change this code in order to insert to the last line of text file? please help, thank you :( (2 Replies)
Discussion started by: gavin_L
2 Replies

6. Shell Programming and Scripting

Add text to specified line

Hi All, I am writing a script in which i need to add text "ABC" at line 14 in file input.txt Can someone please help me how to do it? Thanks. Regards, Tushar (6 Replies)
Discussion started by: tushar_shah06
6 Replies

7. Shell Programming and Scripting

Add text at the end of line conditionally

Hi All, I have a file as below: 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. (14 Replies)
Discussion started by: angshuman
14 Replies

8. Shell Programming and Scripting

Add ; to every line in text file

Please help to add ; to every line in a text file i Have tired sed 's/$/ ; /g' > /tmp/drop_tables.sql but not working :( Thanks (2 Replies)
Discussion started by: bluebird5m
2 Replies

9. Shell Programming and Scripting

how to add text after a particular line on vi editor

hi suppose i have file 0f 10 lines. i want to add something after 8 line. may i append it anyhow without opening file. eg. i can appned at last. echo text >> file Thanks (9 Replies)
Discussion started by: manoj_dahiya22
9 Replies

10. Shell Programming and Scripting

Add text to file at a certain line

I am trying to add a line of text just before the last line in a file. For example, if the last line of a file is "exit 0", I need to add a line of text just before that. Any ideas how I might do that? Thanks (5 Replies)
Discussion started by: TheCrunge
5 Replies
Login or Register to Ask a Question