Extract values from a specific column to the end


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract values from a specific column to the end
# 1  
Old 11-07-2013
Extract values from a specific column to the end

Hello friends,

I have a text file with many columns (no. columns vary from row to row) separated by space. I need to collect all the values from 18th column to the end from each line and group them as pairs and then numbering like below..

1. 18th-col-value 19th-col-value 2. 20th-col-value 21st-col-value 3. 22nd-col-value 23rd-col-value 4.....
1. 18th-col-value 19th-col-value 2. 20th-col-value 21st-col-value 3. 22nd-col-value 23rd-col-value 4.....
1. 18th-col-value 19th-col-value 2. 20th-col-value 21st-col-value 3. 22nd-col-value 23rd-col-value 4.....
......
......
........

Please advise!

Thanks a lot!!
# 2  
Old 11-07-2013
Since you've been following this forum for several years and have posted well over 150 messages, please show us what you've tried so far to convert the input you have to the format you want.
# 3  
Old 11-07-2013
Thanks Don for the reply!

I think my requirement is related with sed/awk which are always mysterious to me.

My initial requirement was to extract only 4 fields from 18th col so I could manage it by looping every line
Code:
cat filename | while read line

and hardcoded them using
Code:
echo 1. awk  '{print $18 $19}' "${line}" 2. awk  '{print $20 $21}' "${line}"

but now we have to extract till the end (probably we need something like a loop with awk using "$").

I wrote big script and am stuck up with extracting part. Please advise!
# 4  
Old 11-07-2013
Assuming that your input file line lengths don't exceed LINE_MAX limits on your system, the following seems to do what you want:
Code:
awk '{  for(i = 18; i <= NF; i += 2)
                printf("%s%d. %s %s", i == 18 ? "" : " ",
                        (i - 16) / 2, $i, $(i + 1))
        printf("\n");
}' file

If you want to run this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

PS Is there anything in this script you don't understand?

Last edited by Don Cragun; 11-07-2013 at 08:37 PM.. Reason: Add note.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-07-2013
Quote:
Originally Posted by Don Cragun
Assuming that your input file line lengths don't exceed LINE_MAX limits on your system, the following seems to do what you want:
Code:
awk '{  for(i = 18; i <= NF; i += 2)
                printf("%s%d. %s %s", i == 18 ? "" : " ",
                        (i - 16) / 2, $i, $(i + 1))
        printf("\n");
}' file

If you want to run this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

PS Is there anything in this script you don't understand?
Fantastic, Thanks Don! Worked like charm!!

How could you offer the solution so quickly? Please suggest how can I easily learn about awk and sed? Smilie
# 6  
Old 11-07-2013
Quote:
Originally Posted by prvnrk
Fantastic, Thanks Don! Worked like charm!!

How could you offer the solution so quickly? Please suggest how can I easily learn about awk and sed? Smilie
Given your problem statement (with the restrictions I stated in my response), it just seemed obvious to me how to do it.

Look at examples posted here in The UNIX and Linux Forums and figure out what the scripts do. If you can't figure out how they work, ask questions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract values and print at end of each line

In the below perl I am trying to extract and print the values AF1=, the GT value, and F or QUAL diveded by 33 (rounded to the nearest whole #). The GT value is at the end after the GT:PL so all the possibilities are read into a hash h, then depending on the value that is in the line the... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

awk to match file1 and extract specific tag values

File2 is tab-delimeted and I am trying to use $2 in file1 (space delimeted) as a search term in file2. If it is found then the AF= in and the FDP= values from file2 are extracted and printed next to the file1 line. I commented the awk before I added the lines in bold the current output resulted. I... (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Shell Programming and Scripting

Count specific column values

Hi all: quick question! I have the following data that resembles some thing like this: i am tired tired am i what is up hello people cool I want to count (or at least isolate) all of the unique elements in the 2nd column. I have tried this: cut -f 2 | uniq 'input' which does... (3 Replies)
Discussion started by: owwow14
3 Replies

4. Shell Programming and Scripting

Generate Codes based on start and End values of numbers in a column

Hello All, Could you please help with this. This is what I have: 506234.222 2 506234.222 2 506234.222 2 506234.222 2 508212.200 2 508212.200 2 333456.111 2 333456.111 2 333456.111 2 333456.111 2 But this is what I want: 506234.222 1 506234.222 2 506234.222 2 506234.222 3 (5 Replies)
Discussion started by: canimba
5 Replies

5. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

6. Shell Programming and Scripting

extract values from column with Perl

Hi everybody I have some problems with PERL programming. I have a file with two columns, both with numeric values. I have to extract the values > 50 from the 2nd columns and sum them among them. The I have to sum the respective values in the first column on the same line and, at the end, I... (6 Replies)
Discussion started by: m_elena
6 Replies

7. UNIX for Dummies Questions & Answers

Extract a specific number from an XML file based on the start and end tags

Hello People, I have the following contents in an XML file ........... ........... .......... ........... <Details = "Sample Details"> <Name>Bob</Name> <Age>34</Age> <Address>CA</Address> <ContactNumber>1234</ContactNumber> </Details> ........... ............. .............. (4 Replies)
Discussion started by: sushant172
4 Replies

8. Shell Programming and Scripting

to extract specific values twice in a file

Hi Friends, I have a file with the following values.. xyz.txt,12345.xml abc.txt,04567.xml cde.txt,12134.xml I would like to extract all the 2nd column values twice as shown in the example like 12345,12345.xml 04567,04567.xml 12134,12134.xml Please advice!! In the formus one of... (7 Replies)
Discussion started by: techmoris
7 Replies

9. Shell Programming and Scripting

How to extract first column with a specific character

Hi All, Below is the sample data of my files: O|A|571000689|D|S|PNH|S|SI sadm|ibscml1x| I|A|571000689|P|S|PNH|S|SI sadm|ibscml1x| O|A|571000689|V|S|PNH|S|SI sadm|ibscml1x| S|C|CAM|D|S|PNH|R|ZOA|2004 bscml1x| ... (3 Replies)
Discussion started by: selamba_warrior
3 Replies

10. Shell Programming and Scripting

I need to extract last column of a file and compare the values

Hi, I am new to unix and I need help in solving below mentioned issue, really appreciate ur help. I have a file sam, john, 2324, 07142007 tom, thomson, 2343, 07142007 john, scott, 2478, 07142007 its a comma delimited file, I need to extract the last column from each line and this... (4 Replies)
Discussion started by: vukkusila
4 Replies
Login or Register to Ask a Question