sed insert into line 1 via script


 
Thread Tools Search this Thread
Operating Systems Solaris sed insert into line 1 via script
# 1  
Old 12-16-2015
sed insert into line 1 via script

Hi
I am trying to run a sed command within a script..unfortunately it wasn't written on Solaris so doesn't work. Can anyone help with the correct coding please?
It is:
Code:
sed -i '1i ROWID;ORDER_ID;JOB_NAME;ORDER_TABLE' ${OUTFILE}


Last edited by vbe; 12-16-2015 at 12:03 PM.. Reason: code tags - not icode
# 2  
Old 12-16-2015
Hi, try something like:
Code:
sed '1s/^/ROWID;ORDER_ID;JOB_NAME;ORDER_TABLE\
/' "$OUTFILE" > "$OUTFILE.tmp" && mv "$OUTFILE.tmp" "$OUTFILE"

# 3  
Old 12-16-2015
Unix sed needs a new line after the i command
Code:
sed '1i\
ROWID;ORDER_ID;JOB_NAME;ORDER_TABLE' ${OUTFILE} > ${OUTFILE}.new &&
cp ${OUTFILE}.new ${OUTFILE} &&
rm ${OUTFILE}.new

(and needs a shell that can handle multi-line i.e. any standard shell).
The output must be copied to a temporary file, then copied back to the input file.
Or
Code:
{
echo "ROWID;ORDER_ID;JOB_NAME;ORDER_TABLE"
cat ${OUTFILE}
} > ${OUTFILE}.tmp &&
cp ${OUTFILE}.tmp ${OUTFILE} &&
rm ${OUTFILE}.tmp

The cp method is very safe because it keeps the inode(mode,permissions,links).
A replacement for sed -i (that breaks links) is perl -i
Code:
perl -i -lpe 'unless (defined $n) {print "ROWID;ORDER_ID;JOB_NAME;ORDER_TABLE"; $n=1}' ${OUTFILE}


Last edited by MadeInGermany; 12-16-2015 at 08:05 PM.. Reason: added the missing .new - thanks Don!
# 4  
Old 12-16-2015
Quote:
Originally Posted by MadeInGermany
Unix sed needs a new line after the i command
Code:
sed '1i\
ROWID;ORDER_ID;JOB_NAME;ORDER_TABLE' ${OUTFILE} > ${OUTFILE}.new &&
cp ${OUTFILE}.new ${OUTFILE} &&
rm ${OUTFIELD}

(and needs a shell that can handle multi-line i.e. any standard shell).
... ... ...
I assume you meant:
Code:
sed '1i\
ROWID;ORDER_ID;JOB_NAME;ORDER_TABLE' ${OUTFILE} > ${OUTFILE}.new &&
cp ${OUTFILE}.new ${OUTFILE} &&
rm ${OUTFIELD}.new

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 12-16-2015
A .bak of the old file is created as ${OUTFILE}.bak for safety. If that's not necessary use just -i
Code:
perl -i.bak -pe '$. == 1 and print "ROWID;ORDER_ID;JOB_NAME;ORDER_TABLE\n"' ${OUTFILE}

This User Gave Thanks to Aia 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

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

2. UNIX for Dummies Questions & Answers

Using sed to insert at position x and then every interval y for each line

Thanks to help from Don Cragun in post 302924174, I'm off and getting into trouble on my own (finally) with sed. Here is my goal - insert \\r\n at the 60th character on each line and then every 76th character thereafter: Input:... (3 Replies)
Discussion started by: gusbrown
3 Replies

3. Shell Programming and Scripting

sed and awk to insert a line after a para

hi I am having a file like this ############################## mod1 ( a(ll) , b( c), try(o) , oll(ll) go(oo) , al(ll) mm(al) , lpo(kka) kka(oop) ); mod2 ( jj(ll) , c( kk), try1q(o1) , ofll(lll) gao(oo1) , ala(llaa) mmf(adl) , lddpo(kkad) kkda(oodp) );... (20 Replies)
Discussion started by: kshitij
20 Replies

4. Shell Programming and Scripting

sed - How to insert line before the first blank line following a token

Hello. I have a config file (/etc/my_config_file) which may content : # # port for HTTP (descriptions, SOAP, media transfer) traffic port=8200 # network interfaces to serve, comma delimited network_interface=eth0 # set this to the directory you want scanned. # * if have multiple... (6 Replies)
Discussion started by: jcdole
6 Replies

5. Shell Programming and Scripting

Insert @Line # - SED (non-GNU)

Just posted on another fellow's question using ed, but I wanted to know about doing it with sed on Unix. For example - I have a file of an unknown length, but I want to add a line after the shell declaration (Line 2). If possible, I'd like the example to be able to just substitute in a number and... (2 Replies)
Discussion started by: Vryali
2 Replies

6. UNIX for Advanced & Expert Users

Not able to insert new line using sed in Sun Solaris

Hi Guys, can any one know how to replace the string by newline using sed ? i dont need any other solution. I am very curious to know this in sed. Input: one,two,three Output one two three we can acheive this by awk but in sed ?, awk '{gsub(/\,/,"\n");print;}' File (6 Replies)
Discussion started by: rangarasan
6 Replies

7. Shell Programming and Scripting

Using sed to insert text file at first line

sed '1r file.txt' <source.txt >desti.txt This example will insert 'file.txt' between line 1 and 2 of source.txt. sed '0r file.txt' <source.txt >desti.txt gives an error message. Does anyone know how 'sed' can insert 'file.txt' before the first line of source.txt? (18 Replies)
Discussion started by: psve
18 Replies

8. AIX

insert a line with variables using sed

Hi, I have to insert a line having variables using sed. But the variables are not getting substituted within sed. Ex: n=2 sed $n' i\ hi' file This works. But the below code does not work. n=2 line=hello sed $n' i\ $line' file The above code inserts '$line' in the 2nd line of the... (9 Replies)
Discussion started by: sugan
9 Replies

9. Shell Programming and Scripting

sed insert text at particular line

I know that sed -n '12p' file will print line 12 but how might I insert text to a specified line? thanks (2 Replies)
Discussion started by: action_owl
2 Replies

10. Shell Programming and Scripting

sed - how to insert chars into a line

Hi I'm new to sed, and need to add characters into a specific location of a file, the fileds are tab seperated. text <tab> <tab> text <tab> text EOL I need to add more characters to the line to look like this: text <tab> <tab> newtext <tab> text <tab> text EOL Any ideas? (2 Replies)
Discussion started by: tangentviper
2 Replies
Login or Register to Ask a Question