Compare Field in Current Line with Field in Previous


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Compare Field in Current Line with Field in Previous

Hi Guys

I have the following file

Quote:
1,A
2,A
3,A
4,B
5,B
6,C
Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above:

Quote:
1,A
2,A
3,A
Quote:
4,B
5,B
Quote:
6,C
Basically, I want to print the lines of the file as long as the second field of the current line is equal to the second field of the previous line. And i want to print them in a file called "second field".txt

Any help appreciated. Thanks.
# 2  
Try:
Code:
awk -F, '{print > $2".txt"}' file

# 3  
Thanks Bartus

I found something similar in the meantime

Code:
awk -F',' '{print> $NF}' file

However i'm getting the following error:

Code:
awk: too many output files 10

i.e. awk creates rightly the first 10 files but stopped after that

does anybody have any idea?

---------- Post updated at 11:18 AM ---------- Previous update was at 11:15 AM ----------

Ok i fixed it using nawk

---------- Post updated at 11:28 AM ---------- Previous update was at 11:18 AM ----------

Actually it's better with nawk but not entirely fixed

i am getting another error now

Code:
nawk: makes too many open files

can anybody help?
# 4  
What kind of os are you on...your last error makes no sense.
# 5  
Solaris 10 11/06
# 6  
Quote:
Originally Posted by moutaye
Solaris 10 11/06
On Solaris use nawk instead of awk and how many input/output files are being generated...
# 7  
thanks shamrock. you probably didn't see my earlier post but i already found that nawk helped

that said i'm having the following issue now:

Code:
nawk: makes too many open files

looks like nawk stops after 20 files...
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #224
Difficulty: Easy
In September 2019, according to NetMarketShare, Windows had just over 87% of the global desktop market, followed by Linux at close to 10%, and macOS in third place at around 2%.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

2. Shell Programming and Scripting

How to compare the current result with previous line result.?

Hi Gurus, I have requirement to compare current result with previous reuslt. The sample case is below. 1 job1 1 1 job2 2 1 job3 3 2 job_a1 1 2 job_a2 2 2 job_a3 3 3 job_b1 1 3 job_b2 2 for above sample file, GID is group ID, for input line, the job run... (1 Reply)
Discussion started by: ken6503
1 Replies

3. Shell Programming and Scripting

Replace first field of a line with previous filed of the line

Hi Everyone, I have a file as below: IM2345638,sherfvf,usha,30 IM384940374,deiufbd,usha,30 IM323763822,cdejdkdnbds,theju,15 0,dhejdncbfd,us,20 IM398202038,dhekjdkdld,tj,30 0,foifsjd,u2,40 The output i need is as below IM2345638,sherfvf,usha,30... (4 Replies)
Discussion started by: usha rao
4 Replies

4. Shell Programming and Scripting

Compare two CSV files and put the difference in third file with line no,field no and diff value.

I am having two csv files i need to compare these files and the output file should have the information of the differences at the field level. For Example, File 1: A,B,C,D,E,F 1,2,3,4,5,6 File 2: A,C,B,D,E,F 1,2,4,5,5,6 out put file: (12 Replies)
Discussion started by: karingulanagara
12 Replies

5. Shell Programming and Scripting

Plz Help. Compare 2 files field by field and get the output in another file.

Hi Freinds, I have 2 files . one is source.txt and second one is target.txt. I want to keep source.txt as baseline and compare target.txt. please find the data in 2 files and Expected output. Source.txt 1|HYD|NAG|TRA|34.5|1234 2|CHE|ESW|DES|36.5|134 3|BAN|MEH|TRA|33.5|234... (5 Replies)
Discussion started by: i150371485
5 Replies

6. Shell Programming and Scripting

Compare two files Field by field and output the result in another file

Hi Friends, Need Help. I have file1.txt as File1.txt |123|A|7267|Hyder|Cross|Sell|7801 |995|A|7051|2008|Lunar|New|Year|Promotion|7801 |996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801 |997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801 File2.txt... (7 Replies)
Discussion started by: i150371485
7 Replies

7. Shell Programming and Scripting

sed to replace a field from a line with another field

i have something like this, cat filename.txt hui this si s"dfgdfg" omeone ipaddress="10.19.123.104" wel hope this works i want to replace only 10.19.123.104 with different ip say 10.19.123.103 i tried this sed -i "s/'ipaddress'/'ipaddress=10.19.123.103'/g" filename.txt ... (1 Reply)
Discussion started by: vivek d r
1 Replies

8. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

9. Shell Programming and Scripting

Print a field from the previous line

plz help me!! I have this file , 3408 5600 3796 6035 4200 6285 4676 0 40 1554 200 1998 652 2451 864 2728 1200 0 I want it like if $2==0,replace it with field from the previous line+500 say here the o/p would be like 3408 5600 3796 6035 4200 6285... (16 Replies)
Discussion started by: Indra2011
16 Replies

10. Shell Programming and Scripting

AWK Compare previous value with current.

Hi, I have one small doubt how to go ahead and process the below requirement. File Content 1,abc,10 2,xyz,11 3,pqr,12 4,pqr,13 5,pqr,14 Output file expected: 1,mnq,1 1,ddd,2 1,qqq,3 1,sss,4 1,ddd,5 1,eee,6 1,fff,7 1,ddr,8 1,rrd,9 (3 Replies)
Discussion started by: dikesm
3 Replies

Featured Tech Videos