AWK - Extracting substr and using it on the same line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK - Extracting substr and using it on the same line
# 1  
Old 12-08-2011
AWK - Extracting substr and using it on the same line

Hey, I'm new to the forums. I've searched around and found some great help on how to work with sub-strings but I'm having problems extracting a substr and then using it on the line it was extracted from Smilie

I have a text file full of a varying amount of tables (each with the same number of rows). Here's and example of one of the tables:
Note: NAME is a random, unknown string that doesn't contain any spaces and varies from table to table
Code:
<TD  bgcolor="#ffffff"> <TD> 1 </TD>
<TD> NAME1</TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
</TR>
<TD  bgcolor="#ffffff"> <TD> 1 </TD>
<TD> NAME2</TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
</TR>
.
.
.
<TD  bgcolor="#ffffff"> <TD> 1 </TD>
<TD> NAMEn</TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
<TD> Random String </TD> 
</TR>

I need to use a bash script to extract each unique NAME and then replace it with:
Code:
 NAME [/file_path/run_program.exe?a=NAME]

Currently I've been starting at line 2 of the file and replacing </TD> every 13 lines although I think the script would be much more versatile if I searched for fff and worked with the succeeding line.

All suggestions are greatly appreciated!


Code:
Answer: TBA


Last edited by pyro214; 12-08-2011 at 06:47 PM..
# 2  
Old 12-08-2011
Code:
awk '{sub("NAME","NAME [/file_path/run_program.exe?a=NAME]")}1' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 12-08-2011
Quote:
Originally Posted by bartus11
Code:
awk '{sub("NAME","NAME [/file_path/run_program.exe?a=NAME]")}1' file

Sorry, I should have mentioned that NAME is a varying string as well :/
# 4  
Old 12-08-2011
Code:
awk -v NAME="whatever" '{ sub(NAME,NAME " [/file_path/run_program.exe?a=NAME]")}1' file

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 12-08-2011
Small fix:
Code:
awk -v NAME="whatever" '{ sub(NAME,NAME " [/file_path/run_program.exe?a="NAME"]")}1' file

These 2 Users Gave Thanks to bartus11 For This Post:
# 6  
Old 12-08-2011
Updated what NAME is:

NAME is a random, unknown string that doesn't contain any spaces and varies from table to table
# 7  
Old 12-08-2011
hi

you are most welcome to this education forum site,here you can find lots of thing related to ur education career
and for your query u can contact your freinds.

--------------------------
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk and substr

Hello All; I have an input file 'abc.txt' with below text: 512345977,213458,100021 512345978,213454,100031 512345979,213452,100051 512345980,213455,100061 512345981,213456,100071 512345982,213456,100091 512345983,213457,100041 512345984,213451,100011 I need to paste the first field... (10 Replies)
Discussion started by: mystition
10 Replies

2. Shell Programming and Scripting

HELP : awk substr

Hi, - In a file test.wmi Col1 | firstName | lastName 4003 | toto_titi_CT- | otot_itit - I want to have only ( colones $7,$13 and $15) with code 4003 and 4002. for colone $13 I want to have the whole name untill _CT- or _GC- 1- I used the command egrep with awk #egrep -i... (2 Replies)
Discussion started by: georg2014
2 Replies

3. Shell Programming and Scripting

awk substr

Hello life savers!! Is there any way to use substr in awk command for returning one part of a string from declared start and stop point? I mean I know we have this: substr(string, start, length) Do we have anything like possible to use in awk ? : substr(string, start, stop) ... (9 Replies)
Discussion started by: @man
9 Replies

4. Shell Programming and Scripting

Substr with awk

Hi to all, I'm here again, cause I need your help to solve another issue for me. I have some files that have this name format: date_filename.csv In my shell I must rename each file removing the date so that the file name is filename.csv To do this I use this command: fnames=`ls ${fname}|... (2 Replies)
Discussion started by: leobdj
2 Replies

5. Shell Programming and Scripting

awk substr

HI I am using awk and substr function to list out the directory names in the present working directory . I am using below code ls -l | awk '{ if ((substr($1,1,1)) -eq d) {print $9 }}' But the problem is i am getting all the files and directories listed where as the requirement i wrote... (7 Replies)
Discussion started by: prabhu_kumar
7 Replies

6. Shell Programming and Scripting

Help with awk and substr

I have the following to find lines matching "COMPLETE" and extract parts of it using substr. sed -n "/COMPLETE/p" 1.txt | awk 'BEGIN { FS = "\" } {printf"%s %s:%s \n", substr($3,17,3),substr($6,4,1), substr($7,4,1)}' | sort | uniq > temp.txt Worked fine until the numbers in 2nd & 3rd substr... (5 Replies)
Discussion started by: zpn
5 Replies

7. Shell Programming and Scripting

awk substr

Hi I have multiple files that name begins bidb_yyyymm. (yyyymm = current year month of file creation). What I want to do is look at the files and where yyyymm is older than 1 month I want to remove the file from the server. I was looking at looping through the files and getting the yyyymm... (2 Replies)
Discussion started by: colesga
2 Replies

8. Shell Programming and Scripting

AWK - Extracting matched line

Hi all, I have one more query related to AWK. I have the following csv data: ,qwertyA, field1, field2, field3, field4, field5, field6 ,,,,,,,,,,,,,,,,,,,100,200 ,,,,,,,,,,,,,,,,,,,300,400 ,qwertyB, field1, field2, field3, field4, field5, field6 ,,,,,,,,,,,,,,,,,,,100,200... (9 Replies)
Discussion started by: not4google
9 Replies

9. Shell Programming and Scripting

Extracting specified line from a file using awk

Hi, I am just trying to extract a line at a time from a file using awk and can't yet extract even the first line using the following:- awk '/(^) && (NR==1)/ {print $1}' file1 Is there an obvious silly error here? Thanks (3 Replies)
Discussion started by: sirtrancealot
3 Replies

10. Shell Programming and Scripting

awk substr?

Sorry if this has been posted before, I searched but not sure what I really want to do. I have a file with records that show who has logged into my application: 2003-03-14:I:root: Log_mesg: registered servername:userid. (more after this) I want to pull out the userid, date and time into... (2 Replies)
Discussion started by: MizzGail
2 Replies
Login or Register to Ask a Question