Append variable to only certain lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append variable to only certain lines
# 1  
Old 06-24-2009
Append variable to only certain lines

Hi There!
I'm trying to write a shell script which generates a random hexadecimal number and then appends it to the end of lines XX onwards of a certain file. XX is determined by a certain string on the previous line.

Thus for example, if the input file is
Quote:
This is line 1
This is line 2
This is line 3
SEPARATOR
This is line 4
This is line 5
I search for the string "SEPARATOR" and starting with the next line, keep appending the hex strings. Thus, the output should be
Quote:
This is line 1
This is line 2
This is line 3
SEPARATOR
This is line 4 <RANDOM HEX>
This is line 5 <RANDOM HEX>
I can generate the random hex strings fine, and also find the line number to start appending from, but im having problems with the actual appending.

What is the best way to do this? (The input files could have 10k+ lines)

Thanks!
-orno
# 2  
Old 06-24-2009
Try this:

Code:
awk -v var="<RANDOM HEX>" '/SEPARATOR/{f=1;print;next}f{$0=$0 FS var}1' file

# 3  
Old 06-24-2009
That's perfect

What if I want to append the variable ONLY on the the line 2 lines after the line found with the string SEPARATOR?

i.e. Output should be
Quote:
This is line 1
This is line 2
This is line 3
SEPARATOR
This is line 4
This is line 5 <RANDOM HEX>
This is line 6
# 4  
Old 06-24-2009
Quote:
Originally Posted by orno
That's perfect

What if I want to append the variable ONLY on the the line 2 lines after the line found with the string SEPARATOR?

i.e. Output should be
Should be something like:

Code:
 awk -v var="<RANDOM HEX>" '/SEPARATOR/{print;getline;print;getline;$0=$0 FS var}1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append variable value with sed

I have a requirement where I need to add a variable value based on a pattern match. When I try this it works: sed '/IS_ETL_DEV/a\ ABCD' File1.txt > File1.tmp Now I want to replace IS _ETL_DEV with a variable like $Pattern and replace the values ABCD with a variable $Var the value "$$XYZ=1".... (4 Replies)
Discussion started by: vskr72
4 Replies

2. UNIX for Dummies Questions & Answers

append following lines to 1st line, every 3 lines

I have output like this: USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 ... where USER_ID is a unique user login followed by their login timestamp and... (6 Replies)
Discussion started by: MaindotC
6 Replies

3. Shell Programming and Scripting

Append 0 to a variable

Hi, I have a variable... it can take a value from 1 to 99... now when it is 1 to 9 i want to save it as 01 to 09 in the same variable... can this be done using sed or awk??? a=1 i want it as a=01 Thanks in advance... (8 Replies)
Discussion started by: Nithz
8 Replies

4. UNIX for Dummies Questions & Answers

how do I append new lines to awk variable?

I want to build an array using awk, consisting only of a subset of lines of a file. I know how to have awk assess whether a key phrase is in a particular line, but I can't find anywhere how to then append the line containing that phrase to an array that has previously-found lines also containing... (9 Replies)
Discussion started by: pts2
9 Replies

5. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

6. Shell Programming and Scripting

Append lines in a file

Legends, Please help me out. I have a file abc.txt with the following entries JACK JIL SANDY amer europe pak Now, i want to append the character after each line. but, condition is if it is UPPER case then character appended should be uppercase, else lowercase example: JACKL2... (5 Replies)
Discussion started by: sdosanjh
5 Replies

7. Shell Programming and Scripting

Append lines in a file

Hi All, I have a file separated by , and each line end with ".If the line doesnt end with " then i need to join the current line and the next one and put them in another file a, b, c,d" d,f,g,h k, l m" o,p,q,r,t" ouput : a,b,c,d" d,f,g,h,k,l,m" o,p,q,r,t" (2 Replies)
Discussion started by: gwrm
2 Replies

8. Shell Programming and Scripting

Append zeros before a value as per variable

Hello- I have a variable which contains a number, I need to populate number of zeros before another value as per this variable value. for example: I have variable X whose content is 5, variable Y whose content is 123 Now append number of zeros as per variable X before varible 'Y'... (4 Replies)
Discussion started by: pasupuleti81
4 Replies

9. Shell Programming and Scripting

Append lines with SED

I have the following data in a file and would like to append the lines inside each section 10, 20, 30, 40, etc... onto one line for each of the sections. Is it possible with SED to make this happen?? 10 00039393 DOCK: RECEIVE PART, TAG. D D ... (3 Replies)
Discussion started by: miklacic
3 Replies

10. UNIX for Dummies Questions & Answers

append word to lines

Hi all, Can someone suggest how to append some word to all lines in file. for example word "Honey" to file f1 with lines: Mia Katrin Elizabeth to get Honey Mia Honey Katrin Honey Elizabeth Thanks in advance Givi (3 Replies)
Discussion started by: giviut
3 Replies
Login or Register to Ask a Question