Find and add a field for an input file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find and add a field for an input file
# 1  
Old 07-25-2012
Find and add a field for an input file

Hello Sir/Madam,

I'm new to UNIX and wondering if I can process a find & add using AWK command in UNIX?

requirements: I have to find *.JPG field for each line in the input file and add it as a separate field for each line.. for ex:

0000684631,E,3088017,7 ,NXA114398114,A,,/usr/GLS/AMI/Pepco/748f4460-37de-468e-a9e3-000bd401e38a_C000684631_InstallMtr1-0002.JPG,07-09-2012,11:35,-76.90694,38.8761366666,

When it is done this is how it should look like:
0000684631,E,3088017,7 ,NXA114398114,A,,C000684631_InstallMtr1-0002.JPG,/usr/GLS/AMI/Pepco/748f4460-37de-468e-a9e3-000bd401e38a_C000684631_InstallMtr1-0002.JPG,07-09-2012,11:35,-76.90694,38.8761366666,

My file has 100000 records and I have to process this for each line.

Please suggest how I can do this..
Thank you in advance for you help..

Regards,
Vani
# 2  
Old 07-25-2012
You said find JPG field.. Does it move? Or do you want the substring after the first underscore in field 8? This does the latter:

Code:
awk -F, '{$8=substr($8,1+index($8,"_")) OFS $8}1' infile

# 3  
Old 07-25-2012
Thank you for the quick reply..

Does it move?
No.

Do you want the substring after the first underscore in field 8?
Yes.. I would like determine the "*.JPG" name after the first underscore and copy that as field in each line after double commas..

I might be doing some thing worng.. when I tried your code this is what is prompting..

perseus.gasleak(/usr/GLS/AMI/Pepco)% awk -F, '{$8=substr($8,1+index($8,"_")) OFS $8}1' phi_pep.bk
awk: syntax error near line 1
awk: bailing out near line 1


---------- Post updated at 11:59 AM ---------- Previous update was at 09:33 AM ----------

awk '{print $2=substr($2,1+index($2,"_"))}' phi_pep.txt

output text is:
C000684631_InstallMtr1-0002.JPG,07-09-2012,11:35,M,-76.90694,38.8761366666,


How can chop off the data after "C000684631_InstallMtr1-0002.JPG," field using your example?
# 4  
Old 07-25-2012
Is this Solaris? use nawk and see that output is as desired?

Code:
$ cat input
0000684631,E,3088017,7 ,NXA114398114,A,,/usr/GLS/AMI/Pepco/748f4460-37de-468e-a9e3-000bd401e38a_C000684631_InstallMtr1-0002.JPG,07-09-2012,11:35,-76.90694,38.8761366666,
$ nawk -F, '{$8=substr($8,1+index($8,"_")) OFS $8}1' OFS=, input
0000684631,E,3088017,7 ,NXA114398114,A,,C000684631_InstallMtr1-0002.JPG,/usr/GLS/AMI/Pepco/748f4460-37de-468e-a9e3-000bd401e38a_C000684631_InstallMtr1-0002.JPG,07-09-2012,11:35,-76.90694,38.8761366666,

# 5  
Old 07-25-2012
it is a UNIX server
# 6  
Old 07-25-2012
"What operating system are you running?"
"UNIX"

"What model is your car?"
"Blue"

Both very vague answers to the question.

If you don't know what your system is, uname -a
# 7  
Old 07-25-2012
All I know is my test server is a UNIX server, but here an appropriate answer for ur qes:

SunOS perseus 5.10 Generic_144500-19 sun4v sparc SUNW,Sun-Blade-T6320

---------- Post updated at 02:37 PM ---------- Previous update was at 12:38 PM ----------

Scott,
Appriciate your prompt help..

nawk worked..

since my file has about 100000 records, would it be safe to write to a different output file instead of printing to screen? if so, please tell me how I can do that?


if you have time, please help me understand below two questions..

Two questions..
1) when I used AWK to print first column


perseus.gasleak(/usr/GLS/AMI/Pepco/Portable)% awk '{print $1}' phi_pep.txt

output is:
0000684631,E,3088017,7

perseus.gasleak(/usr/GLS/AMI/Pepco/Portable)% awk '{print $2}' phi_pep.txt

Why it is displaying like this?

2) if "-F" is used for comma delimited record file, why didn't is print just the first column?



output is:
,NXA114398114,A,,/usr/GLS/AMI/Pepco/Portable/748f4460-37de-468e-a9e3-000bd401e38a_C000684631_InstallMtr1-0002.JPG,07-09-2012,11:35,M,-76.90694,38.8761366666,
This User Gave Thanks to V1l1h1 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find pattern in first field of file

Hello all I have two files. 1. Pattern.txt - It contains patterns to be matched. It has large number of patterns to be matched. Cat Pattern.txt Ram Shyam Mohan Jhon I have another file which has actual data and records are delimted by single or multiple spaces. 2.... (8 Replies)
Discussion started by: krsnadasa
8 Replies

2. Shell Programming and Scripting

Find the position of a field/column in a flat file

Hi, Let say I have a file which has around 400 fields. SampleFile ========= PATIENTID|FACILITY|................|TIME_LAST_VISITED_BY_MD|.....|STATUS| How is it possible to find out which field is TIME_LAST_VISITED_BY_MD?fro example by seeing the above structure we can saw FACILITY... (5 Replies)
Discussion started by: machomaddy
5 Replies

3. Shell Programming and Scripting

find and replace in a directory using an input file

Hi folks, I need help to finish this script please. see below: I have an input file with all the IP address to names formated like so in a txt file addnsr1pri 166.7.3.105 addnsr1sec 166.2.100.22 addnsr2pri 166.2.220.121 addnsr2sec 166.3.68.45... (12 Replies)
Discussion started by: richsark
12 Replies

4. Shell Programming and Scripting

Find a word in input file

Hi, I had a input file containing, abcdefghij;20100903040607;1234567891;GLOBAL; abc123;20100903040607;12345;09;thestdf; def456;20100903040607;67891;04;bnkim; I need to search word GLOBAL in my file. If it is found then do something else do other thing How can i write if loop for... (13 Replies)
Discussion started by: Poonamol
13 Replies

5. Shell Programming and Scripting

How to check field formatting of input file?

Hi, I had input file with below data, abcdefghij;20100903040607;1234567891;GLOBAL; Having values of fields with seperated by semi-colon (;) and ended with line feed (\n). Through shell script, how can I check the field formatting? Thanks in advance. (18 Replies)
Discussion started by: Poonamol
18 Replies

6. UNIX for Dummies Questions & Answers

find uniq lines in file, using the first field of line

Hello all, new to unix and have just found the forum. I think I will be here quite often, and hope that in time i will be able to provide soem help, role on not being a newbie anymore :) I have a question which iI am hoping someone could help me with. If i have a file with lines in in thus... (8 Replies)
Discussion started by: grom
8 Replies

7. Shell Programming and Scripting

Find top N values for field X based on field Y's value

I want to find the top N entries for a certain field based on the values of another field. For example if N=3, we want the 3 best values for each entry: Entry1 ||| 100 Entry1 ||| 95 Entry1 ||| 30 Entry1 ||| 80 Entry1 ||| 50 Entry2 ||| 40 Entry2 ||| 20 Entry2 ||| 10 Entry2 ||| 50... (1 Reply)
Discussion started by: FrancoisCN
1 Replies

8. Shell Programming and Scripting

Find lines in text file with certain data in first field

Hi all, Sorry for the title, I was unsure how to word my issue. I'll get right to the issue. In my text file, I need to find all lines with the same data in the first field. Then I need to create a file with the matching lines merged into one. So my original file will look something like... (4 Replies)
Discussion started by: rstev39147
4 Replies

9. Shell Programming and Scripting

How To Find Length of a Field in XML File

Hi I have a xml file with below data...have to find the length of the filedvalues... <?xml version="1.0" encoding="ISO-8859-15" standalone="no"?><abc xmlns:xsi="http://www.w3.org/2000/XMLSchem... (3 Replies)
Discussion started by: naughty21
3 Replies

10. Shell Programming and Scripting

Find script with input pattern file

Howdy: I have a file with 140+ file name patterns. Each prefix can have dozens of files with different extension names. e.g. 1-S51 1113-G6V 1117-G6V 1119-G6V 1127-G6V 12XW-AF5W 14-UA8N I need to search in 12 directories, (/data/lgc1/basin_mas to /data/lgc12/basin_mas) for all the... (8 Replies)
Discussion started by: iguanathompson
8 Replies
Login or Register to Ask a Question