Help with scripting-->Inserting a line before a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with scripting-->Inserting a line before a pattern
# 1  
Old 05-28-2014
Help with scripting-->Inserting a line before a pattern

Hi Guys,

I have written the following script test.sh in Linux .

Code:
read -p "Please enter the name of the application: " DIRTOCREATE

read -p "Please enter the number of associates to be given access to svn:" COUNT

for (( i=0 ; i<$COUNT ; i++ ))

do

read -p "Enter the associate id  :" ID

echo  $ID >> result.txt

Lets assume the DIRTOCREATE = Testing and the
result.txt contains the below

12345
67891

I want to convert the contents in result.txt and Testing in the format

Testing = 12345, 67891

Finally want to insert the above line just before "[/]" in a file say access.txt which is of format

AppConfigurator = 260812, 260812, 291758, 266551
1713 = 258028, 271602, 407682, 407356
LHS = 291758, 260812

[/]

In short , the output should be ,

AppConfigurator = 260812, 260812, 291758, 266551
1713 = 258028, 271602, 407682, 407356
LHS = 291758, 260812
Testing = 12345, 67891
[/]


Doable ???

Thanks Guys

Thanks.
# 2  
Old 05-28-2014
Try
Code:
read -p "Please enter the name of the application: " DIRTOCREATE
printf "%s %s " $DIRTOCREATE "=" >result.txt
TMP=""
read -p "Please enter the number of associates to be given access to svn:" COUNT
for (( i=0 ; i<$COUNT ; i++ ))
do
read -p "Enter the associate id  :" ID
printf "%s%s" $TMP $ID >>result.txt
TMP=","
done
printf "\n" >>result.txt

and then
Code:
awk '/\[\/\]/ {getline X < "result.txt"; print X}1' file

EDIT: or
Code:
sed '1{x;d}; /\[\/\]/{x;G}' result.txt file


Last edited by RudiC; 05-28-2014 at 01:02 PM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-28-2014
Thanks a lot RudiC..Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Shell Scripting , need to search and print a line that contains a specific pattern

Take example of below file. abc.txt nas1:/abc/test/test1 /test nas1:/abc/test/test1/test2 /test/abc nas1:/abc/test/ Now i have a variable that contains "nas1:/abc/test/test1" value , so i need to search the above file for this variable and print only this line. ... (14 Replies)
Discussion started by: mohit_vardhani
14 Replies

3. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

4. Shell Programming and Scripting

Inserting comma after a specific pattern

Hi Am using ksh shell I have a file like XYSDHY read write; permission; complete; DEGHLO RTERI dersa; upomxi oidewmiew; jdipowj;Now i need to insert semicolon after the BLOCK LETTER patterns and delete all blank lines. Now I need the output as XYSDHY; read write; permission;... (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

5. Shell Programming and Scripting

Commenting a specific line and inserting a new line after commented line.

Hello All, I have following file contents cat file #line=aaaaaa #line=bbbbbb #line=cccccc #line=dddddd line=eeeeee #comment=11111 #comment=22222 #comment=33333 #comment=44444 comment=55555 Testing script Good Luck! I would like to comment line line=eeeeee and insert a new line... (19 Replies)
Discussion started by: manishdivs
19 Replies

6. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

7. Shell Programming and Scripting

Inserting file content into a searched pattern

Hi, i have to insert the content of source.txt into the searched pattern of the file second.txt. $cat source.txt One Two Three . . $cat second.txt This is second file pattern match start here pattern match end here end of the file so the result will be like this (4 Replies)
Discussion started by: posix
4 Replies

8. Shell Programming and Scripting

Shell Scripting:Fetching content from each line with respect to pattern.

one.txt ONS.820.log:V 20Oct2010:GP ^ ^ ONS.123.log:V 21Oct2010:GP ^ ^ ONS.820.log:V 30Oct2010:GP ^ ^ want to make new file from existing one with addition. 20Oct2010 User KV001 has name tk003 with buffer- 338-1 21Oct2010 User KV003 has name tk002 with buffer- 338-2 30Oct2010 User KV002... (5 Replies)
Discussion started by: saluja.deepak
5 Replies

9. Shell Programming and Scripting

Inserting a line in a file after every alternate line

Friends , I have a large file and i need to insert a line after every line.I am actually unaware how to do it.Any help appreciated. My File control station *ATM* , qread $OSS.Jul13A.FI01 interval 1 intcount 1 control station *ATM* , qread $OSS.Jul13A.FI02 interval 1 intcount... (4 Replies)
Discussion started by: appu2176
4 Replies

10. UNIX for Advanced & Expert Users

Inserting a line before the line which matches the patter

Hi Is there any command where we can insert a line "2|||" before every line starting with "3|" my input is as follows 1|ETG|12345 3|79.58|||GBP|| 1|ETG|12345 3|79.58|||GBP|| 1|ETG|12345 2|EN_GB||Electrogalvanize 0.5 m2 ( Renault ) 1|ETG|12345 3|88.51|||GBP|| desired output... (10 Replies)
Discussion started by: laxmi131
10 Replies
Login or Register to Ask a Question