How to insert/append line using hamilton cshell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert/append line using hamilton cshell
# 1  
Old 05-21-2009
How to insert/append line using hamilton cshell

Hi everyone

I just need a script that should just search for a particular keyword in a line and after that it should add some text to the next line.

I have tried it in HAMILTON CSHELL using

sed "hello/a\newtext" file.txt

But it give me message

sed: "\" must terminate the "a" command.

Can any one please help me.

Thanks in advance
Sarbjit
# 2  
Old 05-21-2009
Code:
 sed '/hello/a\
 newtext' file  [ this is in the next line]

# 3  
Old 05-22-2009
But when i hit enter it says csh : couldn't find ending ' quote

Actually this thing is working fine on linux os, but i am using hamilton cshell installed on win xp . So i just need to ask is syntax diffferent for hamilton
# 4  
Old 05-31-2009
Using Hamilton C shell and sed to insert text

Hi, I'm the author of Hamilton C shell. The reason for the different behavior under Linux than under my C shell is that I followed the POSIX 1003.2 standard for sed and the other utilities. Linux doesn't, or at least, not always.

Under the POSIX standard, as someone else already pointed out, the text you'd like to add using sed should start on a new line and each embedded newline should be preceded by a backslash. You can do that in a variety of ways.

From the C shell command line, use the ^n escape sequence. (I'm assuming you're using the default Windows-style setting for the escape character.)
65 C% cat > DaysOfWeek
Monday
Tuesday
Wednesday
Thursday
Friday
^Z
66 C% sed '/Friday/a\^nSaturday\^nSunday' DaysOfWeek
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
67 C% _

From the command line, escaping the Enter key. (My C shell doesn't just embed newlines if you type Enter in the middle of a quoted string; you have to escape them.)

67 C% sed '/Friday/a\^
Saturday\^
Sunday' DaysOfWeek
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
70 C% _
In a script:
70 C% cat >AddWeekend
/Friday/a\
Saturday\
Sunday
^Z
71 C% sed -f AddWeekend DaysOfWeek
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
72 C% _
Hope this helps.
# 5  
Old 06-01-2009
Quote:
Originally Posted by Nicole Hamilton
Hi, I'm the author of Hamilton C shell. The reason for the different behavior under Linux than under my C shell is that I followed the POSIX 1003.2 standard for sed and the other utilities. Linux doesn't, or at least, not always.

Under the POSIX standard, as someone else already pointed out, the text you'd like to add using sed should start on a new line and each embedded newline should be preceded by a backslash. You can do that in a variety of ways.

From the C shell command line, use the ^n escape sequence. (I'm assuming you're using the default Windows-style setting for the escape character.)
65 C% cat > DaysOfWeek
Monday
Tuesday
Wednesday
Thursday
Friday
^Z
66 C% sed '/Friday/a\^nSaturday\^nSunday' DaysOfWeek
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
67 C% _

From the command line, escaping the Enter key. (My C shell doesn't just embed newlines if you type Enter in the middle of a quoted string; you have to escape them.)

67 C% sed '/Friday/a\^
Saturday\^
Sunday' DaysOfWeek
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
70 C% _
In a script:
70 C% cat >AddWeekend
/Friday/a\
Saturday\
Sunday
^Z
71 C% sed -f AddWeekend DaysOfWeek
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
72 C% _
Hope this helps.
Thanks Nicole for your solution , Now i am able to insert / append using hamilton.Smilie

-----Post Update-----

HI Nicole,

Is it possible to use same syntax in csh script.

Now my problem is that i need to append a text file using sed. So i make following script, but in this case when i run it , it says that sed \ must terminate a command

#!/bin/csh -f
foreach file (`ls *.txt`)
sed "/Some_Text/a\^n NEW_TEXT" $file
end

Actually i tried the same syntax with proper file name in command prompt and it works there.

Can you please help me.
Thanks

Regards
Sarbjit

-----Post Update-----

Hi Nicole,

I tried the following and then i am able to use sed in csh script.

eg:

sed -f script file.txt

and in script i have written
/SOME TEXT/a\
NEW TEXT

and it works

Now my question is why i am not able to use

sed '/SOME TEXT/a\^n NEW TEXT' file.txt in csh script , but if i use same syntax in hamilton command prompt it works fine.

appreciated your help in advance

Regards
Sarbjit Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove new line starting with a numeric value and append it to the previous line

Hi, i have a file with multiple entries. After some tests with sed i managed to get the file output as follows: lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1, 00,2110597,2094790,0,81,529,75649011,56435363, lsn=TM1ITP1-AM1ITP1-LS,apc=500:0,state=avail,avail/links=1/1,... (5 Replies)
Discussion started by: nms
5 Replies

2. Shell Programming and Scripting

Using sed to find and append or insert on SAME line

Hi, $ cat f1 My name is Bruce and my surname is I want to use SED to find “Bruce” and then append “ Lee” to the end of the line in which “Bruce” is found Then a more tricky one…. I want to INSERT ….a string… in to a line in which I find sometihng. So example $ cat f2 My name is... (9 Replies)
Discussion started by: Imre
9 Replies

3. Shell Programming and Scripting

Append Next line with current Line bassed on condition

Hi, I have an XML file and I am tring to extract some data form it, after lot of data cleaning process, I ended up with an issue, and need your urgent support. my current input data in below format: <Node>xxxxxx <Node>yyyyy</Node> <Node>zzzzzz <Node>12345</node> I need... (9 Replies)
Discussion started by: rramkrishnas
9 Replies

4. Shell Programming and Scripting

Reading file line by line in cshell

I want to read a file in cshell line by line. But it is reading the values by spaces.... For Ex, my file1 :word1 word2 word3 word5 word6 By below script, variable taking the values separated by spaceforeach v ( `cat file`) echo $v end the output will be like,word1 word2 word3... (2 Replies)
Discussion started by: arup1980
2 Replies

5. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

6. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "Ç"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 Ç890000000 Ç543209911 ÇCHNGOHG Ç000000001 Ç055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

7. Shell Programming and Scripting

cshell reading file line by line

hey all, I have this code : #!/bin/tcsh -f set line=($<) while($#line > 0) echo $line set line=($<) end the usage to run the script would be : ./file < file2 Is there a way to specify filename in the script itself instead of getting/reading file name from STDIN ? (1 Reply)
Discussion started by: eawedat
1 Replies

8. Shell Programming and Scripting

How to change a number on a specific line with cshell or shell?

Hello all, I need to change a number in a file by adding some residuals respectively To make it clear, I need to add 0.11 to the number between 24-28 (which is below the SECON) for all the lines starting with FRR1 or I need to add 0.13 to the number between 24-28 (which is below the... (9 Replies)
Discussion started by: miriammiriam
9 Replies

9. 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

10. Shell Programming and Scripting

Shell append or insert

Is there a way to append or insert a word into a text file? for example: if a file contains the lines...... profilename="test_profile" ear="${WpsInstallLocation}/installableApps/" conntype="SOAP" What I would like to have is the word test.ear inserted as below..... ... (4 Replies)
Discussion started by: norman_d_builde
4 Replies
Login or Register to Ask a Question