add new lines of text before and after each input line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers add new lines of text before and after each input line
# 1  
Old 10-10-2008
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

below each line

to produce

MACROBEGIN MACRO_000001
this_is_macro,000001
MACROEND
MACROBEGIN MACRO_000002
this_is_macro,000002
MACROEND
MACROBEGIN MACRO_000003
this_is_macro,000003
MACROEND

Thanks,
Kenny.
# 2  
Old 10-10-2008
Code:
awk -F, '{ print "MACROBEGIN MACRO_" $2 RS $0 RS "MACROEND" }' file


If you need the pattern, only related to the "macro" records use:

Code:
awk -F, '/this_is_macro/{ print "MACROBEGIN MACRO_" $2 RS $0 RS "MACROEND" ;next}1' file


Last edited by rubin; 10-10-2008 at 07:09 PM.. Reason: added second code
# 3  
Old 10-10-2008
this will also do
Code:
awk -F, '{print "MACROBEGIN MACRO_"$2"\n"$0"MACROEND\n"}' filename

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search last column of INPUT.txt in TABLEs text and add correspond columns to INPUT.txt

Hi dears i use bash shell i have INPUT.txt like this number of columns different in one some row have 12 , some 11 columns see last column INPUT.txt CodeGender Age Grade Dialect Session Sentence Start End Length Phonemic Phonetic 63 M 27 BS/BA TEHRANI 3 4 298320 310050... (2 Replies)
Discussion started by: alii
2 Replies

2. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Add input to text file

In the attached bash file I am trying to add a block of code to add2text that will copy the input from match to a text file (file.txt). For example, if from the menu choice 1 is select the user is asked for the id.... lets say that is 12345, after that id is matched and converted can it be added... (22 Replies)
Discussion started by: cmccabe
22 Replies

4. Shell Programming and Scripting

How do I split a single-line input into five lines?

Example input: John:Shepherd:770-767-4040:U.S.A:New York Mo Jo:Jo Jo: 666-666-6666:U.S.A:Townsville Expected Output: First Name: John Last Name: Shepherd Phone Number: 770-767-4040 Country: U.S.A State: New York First Name: Mo Jo Last Name: Jo Jo Phone Number: 666-666-6666... (10 Replies)
Discussion started by: Camrikron
10 Replies

5. Shell Programming and Scripting

Curl - input line by line from text file

Hi, I've got a text file with hundreds of lines I need to upload to an API via curl, one by one. The text file is like: 2012-08-01 10:45,124 2012-08-02 10:45,132 2012-08-03 10:45,114 I want to get curl to go through the text file sending a post for each line. like: curl --request... (0 Replies)
Discussion started by: emdeex
0 Replies

6. Shell Programming and Scripting

Two Input Lines Into Single Output Line (CSV)

Hi all, My search karate must be weak because I'm about certain something very like this has been asked and answered here many times. I'll give you the exact scenario I've wasted a few hours of my Saturday on: :wall: I'm trying to read through a very large number (~200) of router and... (28 Replies)
Discussion started by: svermill
28 Replies

7. Shell Programming and Scripting

input text at certain lines of a file

Hi, I have an xml file which will be edited by the user. I would like to get input from user and insert that at line 40 of the xml file. PLease can some one help me to know how to insert the text at specified line using shell script. (6 Replies)
Discussion started by: sunrexstar
6 Replies

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

9. Shell Programming and Scripting

How to input text on every 2nd line?

I have a file in two columns consisting of only numbers. I would like to put a string (the same textstring) on every second line, so that 501006 420.000 401602 165.000 . . . becomes: *Some_text 501006 420.000 *Some_text 401602 165.000 *Some_text . . . Are you up for it? (7 Replies)
Discussion started by: Medova
7 Replies

10. UNIX for Dummies Questions & Answers

How to read a line of text from user input?

Hiii I wanna a read a line of text from standard input. The user enter data like this way name phone_no month1_salary month2_salary that is user enter the name ,phone no and salary of 2 months in a single line by giving spaces. I wanna add the 3rd and 4th fields ...ie add both... (4 Replies)
Discussion started by: krishnampkkm
4 Replies
Login or Register to Ask a Question