Need to parse file "x" lines at a time ... awk array?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to parse file "x" lines at a time ... awk array?
# 1  
Old 01-27-2010
Need to parse file "x" lines at a time ... awk array?

I have files that store multiple data points for the same device "vertically" and include multiple devices. It repeats a consistant pattern of lines where for each line:

Column 1 is a common number for the entire file and all devices in that file
Column 2 is a unique device number
Column 3 is a unique identifier of the data point included on that line
Column 4 is the unique data point

Code:
# cat myfile.csv
x,y1,a,name1
x,y1,b,2.5
x,y1,c,4
x,y2,a,name2
x,y2,b,3
x,y2,c,5.5
x,y3,a,name3
x,y3,b,1
x,y3,c,2

So above I have three devices (y1, y2 and y3) that all have three data points (a, b and c). One of the data points is a unique name, so I can discard $1,$2,$3 and I only want to retain $4. What I want to do is flatten the three data points into a single line per device:
Code:
name1,2.5,4
name2,3,5.5
name3,1,2

I have found a way to take a given set of lines, awk print $4 and insert them on the same line

Code:
# sed -n 1,3p myfile.csv | awk -F"," '{print $4","}' | tr -d '\n'
name1,2.5,4

But I need a loop to continue processing the next "x" lines.

Above is a simple view of what I'm trying to do. My files have 53 data points for every device. And the number of devices is "random". Therefore my loop that "ingests" 53 lines at a time and then spits them out on a single line needs to continue until the file is complete (do ; done < $1 ?). For example one file is 312,912 lines (5,904 devices x 53 data points) another is 318,000 lines (6,000 devices x 53 data points). Using sed I can do what I need to do on the first 53 lines of the file, but now I just need to insert it into a loop.

Code:
 
# sed -n 1,53p myfile.csv | awk -F"," '{print $4","}' | tr -d '\n'

Any help would be greatly appreciated.


Signed,

Sleepless in Seattle
# 2  
Old 01-27-2010
You could use 'split' to split the file into multiple files of 53 lines each, and then concatenate the results back into a single file.
# 3  
Old 01-27-2010
Try this:
Code:
awk -F, '{if($2==p)d=d","$4;else{print d;d=$4;p=$2}}END{print d}' infile

# 4  
Old 01-27-2010
If the order of the lines are the same:
Code:
awk -F, '{printf("%s%s", $4, NR%3?FS:"\n")}' file

# 5  
Old 01-27-2010
Quote:
Originally Posted by Franklin52
If the order of the lines are the same:
Code:
awk -F, '{printf("%s%s", $4, NR%3?FS:"\n")}' file

That kicked out a syntax error on me. Could be the version of awk I have (Sol10).

Quote:
Originally Posted by Scrutinizer
Try this:
Code:
awk -F, '{if($2==p)d=d","$4;else{print d;d=$4;p=$2}}END{print d}' infile

That worked like a charm! If I interpret this a bit you did not imply anything regarding 53 lines, but instead told it to read all the lines while $2 is constant. Once $2 changes then dump line and start over. Correct?
# 6  
Old 01-27-2010
Quote:
Originally Posted by STN
That kicked out a syntax error on me. Could be the version of awk I have (Sol10).
Use nawk or /usr/xpg4/bin/awk on Solaris.
# 7  
Old 01-27-2010
Quote:
Originally Posted by STN
Correct?
Exactly Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

4. Shell Programming and Scripting

Cant get awk 1liner to remove duplicate lines from Delimited file, get "event not found" error..help

Hi, I am on a Solaris8 machine If someone can help me with adjusting this awk 1 liner (turning it into a real awkscript) to get by this "event not found error" ...or Present Perl solution code that works for Perl5.8 in the csh shell ...that would be great. ****************** ... (3 Replies)
Discussion started by: andy b
3 Replies

5. Shell Programming and Scripting

awk :help to parse a file to change to separated by colon ":"

Hi experts , I am trying to get the below output: file : 0/6/4/1 0x0019503C2E26 5 UP lan5 snap5 1 ETHER Yes 224 0/6/4/0 0x0019503C2E25 6 UP lan6 snap6 2 ETHER Yes 224 0/2/1/0 0x0019503E6900 0 UP lan0 snap0 3 ETHER Yes 224... (8 Replies)
Discussion started by: rveri
8 Replies

6. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. Shell Programming and Scripting

acessing awk array element while getline < "file"

I am attempting to write a awk script that reads in a file after awk array elements are assigned and using those elements while reading in the new file. Does this make sense? /pattern/ {tst=$3} (( getline < "file" ) > 0 ) { x=x " "tst } When I print tst in the END statement it... (9 Replies)
Discussion started by: timj123
9 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question