Add 3 new lines for every line in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add 3 new lines for every line in a file
# 1  
Old 04-16-2014
Add 3 new lines for every line in a file

I have a file that I need to add 3 new lines of text for every line in the file - when I attempt the sed scripting below I keep getting 'command garbled' errors. Any suggestions would be greatly appreciated

Code:
#!/bin/ksh
list=`cat /export/home/list`
for i in $list
 do
sed ' a \
send_notification: F \n
notification_msg: "Process Has Failed "\n
notification_emailaddress: 720394@tmomail.net'  >> list.out
done

# 2  
Old 04-16-2014
I don't know what you're running sed for.

Code:
while read LINE
do
cat <<EOF
send_notification: F \n
notification_msg: "Process Has Failed "\n
notification_emailaddress: 720394@tmomail.net
EOF
done < list.txt > list.out

# 3  
Old 04-16-2014
This worked to a point - it did not pass in the original lines from the list.txt file - just wrote out the 3 new lines for every line in the file - I need all 4 lines in final output. Sorry but I am new to scripting and thought sed was the way to go.
# 4  
Old 04-17-2014
If you would have shown us an input file and what you wanted the output to look like as well as the code you were using to try to get that result, we might have given you something more like:
Code:
#!/bin/ksh
list=/export/home/list
list=list
sed ' a \
send_notification: F \
notification_msg: "Process Has Failed "\
notification_emailaddress: 720394@tmomail.net
' "$list"  > list.out

to start with. I'm still making some wild guesses about what the ends of the three lines you're adding are supposed to look like. With an input file that contains:
Code:
line1
line2
line3
line4

the above script writes:
Code:
line1
send_notification: F 
notification_msg: "Process Has Failed "
notification_emailaddress: 720394@tmomail.net
line2
send_notification: F 
notification_msg: "Process Has Failed "
notification_emailaddress: 720394@tmomail.net
line3
send_notification: F 
notification_msg: "Process Has Failed "
notification_emailaddress: 720394@tmomail.net
line4
send_notification: F 
notification_msg: "Process Has Failed "
notification_emailaddress: 720394@tmomail.net

in list.out. Is this what you're trying to do?
# 5  
Old 04-17-2014
Yes that is exactly what I am trying to do. When I attempted to use your script above I am getting the following error:

sed: command garbled: a \
# 6  
Old 04-17-2014
sed is still not required...

Code:
while read LINE
do
cat <<EOF
$LINE
send_notification: F \n
notification_msg: "Process Has Failed "\n
notification_emailaddress: 720394@tmomail.net
EOF
done < list.txt > list.out

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 04-17-2014
That worked perfectly!!! Thank you so much - you guys rule
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

Read the file line by line and do something with lines

I have a file file_name_O.txt The file can have different number of other files names or nothing I will check cnt=`wc -l file_name_0.txt` if ;then exit 1 fi Now I have to start checking file names, i.e. read txt file line by line. If amount of ,lines equal 1, I can... (4 Replies)
Discussion started by: digioleg54
4 Replies

3. Shell Programming and Scripting

Find all lines in file such that each word on that line appears in at least n lines of the file

I have a file where every line includes four expressions with a caret in the middle (plus some other "words" or fields, always separated by spaces). I would like to extract from this file, all those lines such that each of the four expressions containing a caret appears in at least four different... (9 Replies)
Discussion started by: uncleMonty
9 Replies

4. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

5. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

6. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

7. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

8. Shell Programming and Scripting

Extract some lines from one file and add those lines to current file

hi, i have two files. file1.sh echo "unix" echo "linux" file2.sh echo "unix linux forums" now the output i need is $./file2.sh unix linux forums (3 Replies)
Discussion started by: snreddy_gopu
3 Replies

9. Shell Programming and Scripting

add number in lines line by line in different files

I have a set of log files that are in the following format ======= set_1 ======== counter : 315 counter2: 204597 counter3: 290582 ======= set_2 ======== counter : 315 counter2: 204597 counter3: 290582 ======= set_3 ======== counter : 315 counter2: 204597 counter3: 290582 Is... (6 Replies)
Discussion started by: grandguest
6 Replies

10. UNIX for Dummies Questions & Answers

add new lines of text before and after each input line

I have a file that contains hundreds of lines such as: this_is_macro,000001 this_is_macro,000002 this_is_macro,000003 I would like to add the variable words MACROBEGIN MACRO_000001 MACROBEGIN MACRO_000002 MACROBEGIN MACRO_000003 above each line and add the word MACROEND ... (2 Replies)
Discussion started by: kenneth.mcbride
2 Replies
Login or Register to Ask a Question