keep only pairs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting keep only pairs
# 1  
Old 05-11-2012
keep only pairs

Hi,

I am trying to paste together two files which are like this-

Code:
1 
2
4

and
Code:
2
3
4

The paste command would work by default for this case, but when there are cases where the number of entries are different in each file. for eg:
Code:
1 
3

and
Code:
1
3
4

I want to make it such that the odd entry is removed and put these two together.

Is there a way to do it using sed or awk Smilie

Thanks
# 2  
Old 05-11-2012
Code:
awk '{ A[$1]++ } END { for(X in A) if(A[X]==2) print X }' file1 file2

# 3  
Old 05-11-2012
Thanks a lot Smilie
If you have the time could you give hints on how this works, I find it a bit confusing, I am still getting in grips with awk n sed.

Thanks again.

---------- Post updated at 12:34 AM ---------- Previous update was at 12:29 AM ----------

Hi,
for the case of
Code:
1
2
4
5

and
Code:
2
4
4

I get just 2 as the output.

Just tried this at random.

THanks a lot!
# 4  
Old 05-11-2012
That's because 4 occurs three times, not two. If that's okay, try A[X]>=2 instead of A[X]==2.

It works by counting how many times each letter does, and storing it in an associative array. Each time a number is found, it's incremented. Then it goes through the whole array and prints ones that have been found the appropriate number of times.
# 5  
Old 05-11-2012
Hi,
Thanks again..
I think I should've posted the intended output to make it clear. For the previous input, I expect something like
Code:
1 2
2 4
4 4

# 6  
Old 05-11-2012
In that case, just
Code:
paste file1 file2 | awk 'NF>2'

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 05-11-2012
Here you go: (use nawk with Solaris)
Code:
awk '
NR == FNR { first[FNR]=$0 ; c1++}
NR != FNR { second[FNR]=$0 ; c2++}
END {
  count=c1>c2?c2:c1;
  for(i=1;i<=count;i++)
  {
    printf("%s\t%s\n",first[i],second[i]);
  }
}
' file1 file2


Last edited by jlliagre; 05-11-2012 at 08:08 PM.. Reason: Use tab as separator like paste(1)
This User Gave Thanks to jlliagre For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sorting by pairs

Can sort sort by pair of lines? infile: ID 15 GNJSMKSNS ID 25 GNJSMKSNS ID 1 GNJSMKSNS outfile: ID 1 GNJSMKSNS ID 15 GNJSMKSNS ID 25 GNJSMKSNS sort -grk 2 obviously does not work (4 Replies)
Discussion started by: Xterra
4 Replies

2. UNIX for Dummies Questions & Answers

Concatenate files by pairs

Hi, I would like to batch concatenate files by pairs. I have quite a few of them so I would not like to do that pair by pair separately. the names of the file is of the type: file1.fastq newfile1_new.fastq file2.fastq newfile2_new.fastq and so on... I would like to concatenate file1... (2 Replies)
Discussion started by: jawad
2 Replies

3. Shell Programming and Scripting

Running a diff on all pairs of arguments

I would like to create a loop and an inner loop to do a diff command on each pair of files passed as arguments. For example, if four files were on the command line, a diff would run on 1-2, 1-3, 1-4, 2-3, 2-4, & 3-4. Thus far I know the $# will give the count of the arguments and argv (instead of... (4 Replies)
Discussion started by: wbport
4 Replies

4. Shell Programming and Scripting

Looping through files in pairs

Hi all, Please guide. It has to do with parsing the input file names. I have a fairly large number of files, I want to do some operations on them in a pairwise fashion (every file has a pair). The names are in the following pattern, with the pairs of files named with _1 and _2 , the... (4 Replies)
Discussion started by: newbie83
4 Replies

5. Shell Programming and Scripting

Extracting key/value pairs in awk

I am extracting a number of key/value pairs in awk using following: awk ' /xyz_session_id/ { n=index($0,"xyz_session_id"); id=substr($0,n+15,25); a=$4; } END{ for (ix in a) { print a } }' I don't like this Index + substr with manually calculated... (5 Replies)
Discussion started by: migurus
5 Replies

6. Shell Programming and Scripting

Looping through pairs of files with awk

Hi all, please help me construct the command. i want to loop through all files named bam* and bed*. My awk works for a particular pair but there are too many pairs to do manually. I have generated multiple files in a folder in a given pattern. The files are named like bam_fixed1.bam... (2 Replies)
Discussion started by: newbie83
2 Replies

7. UNIX for Dummies Questions & Answers

Remove Duplicate Two Line Pairs?

So I have a bunch of files that look like this >gi|33332323 MMKCRGVIMVVEKVMKRDGRIVPFDESRIRWAVQ--- >gi|45235353 MMKCR----VEKMRDVFFDESIRWAVQ They go on...sequences are much longer but all in two line (fasta) format. I want to remove duplicate pairs of ID(GI) number and sequence. I tried... (12 Replies)
Discussion started by: bakere19
12 Replies

8. Shell Programming and Scripting

concatenate lines in pairs

Hi, I have a text file with the following contents /C=IT/O=INFN/OU=Personal Certificate/L=Napoli/CN=Some guy /C=IT/O=INFN/CN=INFN CA /O=Grid/O=NorduGrid/OU=uninett.no/CN=Another guy /O=Grid/O=NorduGrid/CN=NorduGrid Certification Authority /C=TW/O=AP/OU=GRID/CN=Someone else... (5 Replies)
Discussion started by: kerl
5 Replies

9. Shell Programming and Scripting

PERL name value pairs substituions

I have a main file with variable tokens like this: name: File1 =========== Destination/Company=@deploy.company@ Destination/Environment=@deploy.env@ Destination/Location=@deploy.location@ Destination/Domain=@deploy.location@ MIG_GatewayAddresses=@deploy.gwaddress@ MIG_URL=@deploy.mig_url@... (1 Reply)
Discussion started by: uandme2k2
1 Replies

10. Shell Programming and Scripting

perl search and replace pairs

Hello all im facing some kind of problem i have this string : functionA() $" "$ functionB("arg1") $" = "$ i will like to replace all the pairs of opening and closing "$" to be something like that functionA() <#" "#> functionB("arg1") <#" = "#> i cant of course do is with simple ... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question