only if column1 equals this print that

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting only if column1 equals this print that
# 1  
Old 04-21-2008
only if column1 equals this print that

I have text file with hundreds of lines, space delimited, each line has the same amount of "columns" and the same amount of characters in each, Column 1, Column 2, and Column 3.
I need a script that will print all columns of the "current" line along with the last two columns of the next line ONLY IF the first columns are equal.
Another way of putting it...

If Column1CurrentLine equals Column1NextLine
print AllOfCurrentLine Column2NextLine Column3NextLine

So my input has 3 columns and my output would have 5 total but only 3 in some lines?

A abc def
A hij klm
A nop qrs
B abc def
B hij klm
C aaa bbb

A abc def hij klm
A hij klm nop qrs
A nop qrs
B abc def hij klm
B hij klm
C aaa bbb

Last edited by Yogesh Sawant; 04-22-2008 at 05:12 AM.. Reason: added code tags
# 2  
Old 04-21-2008
Remember the previous line, and print that if the current line doesn't match. If the current line does match the previous line, print a combined line. As a special case, print the last one you remembered at end of file.

Actually the remembering for the next round has to happen right at the end of the main loop, and the beginning of the loop is conditional on there being a remembered value.

awk '{ if (r[0]) {
    if ($1 != r[0]) print r[0], r[1], r[2]; 
    else print $1, r[1], r[2], $2, $3;
  r[0] = $1; r[1] = $2; r[2] = $3; }
END { print r[0], r[1], r[2]; }' file

Last edited by era; 04-21-2008 at 04:24 PM.. Reason: Improve flow
# 3  
Old 04-21-2008
Here's a Python version.

It expects to be run as such: filename.txt

#!/usr/bin/env python

import sys

file = sys.argv[1]

input = open(file, 'r')

previous = ""

for line in input:

    line = line.rstrip()

    if not previous == "":
        fields = line.split(" ")
        prev = previous.split(" ")
        if fields[0] == prev[0]:

        print " ".join(prev)

    previous = line

print previous

# 4  
Old 04-22-2008
line=`cat filename|wc -l`
nawk -v l="$line" '{
if (t=="")
if(t!="" && $1!=tf)
	print t
else if(t!="" && $1==tf)
	print t" "$2" "$3
}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Do nothing if column1 is found

Hello, I have stucked at one point. When I run the script, I am asking the script to search in database file and if it's found, do nothing and proceed to next line in database file. #!/bin/bash ./ output2 > database while read -r COL1 X=$(grep $COL1 database ) if ]... (7 Replies)
Discussion started by: baris35
7 Replies

2. UNIX for Dummies Questions & Answers

If variable equals string help

Hi All, Trying to get my bash script if statement to work however my if variable equals xxx statement doesnt appear to work, could anyone shed some light. ./script $password &> output INCORRECTPASS=`grep "Permission denied, please try again." output` echo "$INCORRECTPASS" ... (8 Replies)
Discussion started by: mutley2202
8 Replies

3. UNIX for Dummies Questions & Answers

awk if statement / equals operator

Hi, I was hoping someone could explain this please :) I'm using bash, scientific linux... and I don't know what else you need to know. With awk '{ if( 0.3 == 0.1*3) print $1}' file.dat nothing will be printed since apparently the two numbers do not equate. (Using 0.3 != 0.1*3 is seen... (4 Replies)
Discussion started by: Golpette
4 Replies

4. Shell Programming and Scripting

[solved] how to check if two arrays are equals?

how to compare to arrays to check if each elements of the first are the same of the second? for ((i=0;i<$LENGTH;i++)) ; do for (j=0;j<$LENGTH;j++)) ; do if } == ${ARR2} ] echo "Are the same"; fi; done; done; i try this but it doesn't work :( if i make... (0 Replies)
Discussion started by: tafazzi87
0 Replies

5. Shell Programming and Scripting

How to output the partially equals

Hello i have 2 files: a.out james.franco google.gol yahoo.bol b.out I need to see an output just with: james.franco google.gol Thankz in advance!! (2 Replies)
Discussion started by: danielldf
2 Replies

6. Shell Programming and Scripting

awk : Remove column1 and last column in a line

Hi All, How to remove col1 and last column in a line. Please suggest some awk stuffs. Input col1 col2 col3 col4 col1 col2 col3 col4 col5 col1 col2 col3 col4 col1 col2 col3 Output Processing col2 col3 ... Processing col2 col3 col4 ... Processing col2 col3 ... Processing... (5 Replies)
Discussion started by: k_manimuthu
5 Replies

7. Shell Programming and Scripting

Compare 3 files, delete data equals.

Hi, i have a problem, I have three files, file_1, File_2 file_3 and I need to compare the data with file_3 file_1, data that are equal to file_3 file_1 should be deleted, file_1 receive data and file_2 file_3. Ex: file_1 374905,2001, Selmar Santos, Técnico de Sistemas, U$3.000,00 789502,... (3 Replies)
Discussion started by: selmar
3 Replies

8. Shell Programming and Scripting

How TO: if [ Not Equals ] - Solaris 8

Hi, I have the following script which runs well :- ls -l /etc/*.txt > /dev/null 2>&1 if ; then "Success" fi But, if I try, if ; then "Success" fi Does not works ! Even, (4 Replies)
Discussion started by: angshuman_ag
4 Replies

9. Shell Programming and Scripting

Getting rip of multiple rows based on column1

Hi, I want to get rid of multiple rows (duplicate, triplicate etc..) for only column 1. e.g. iu 2 iu 1 iu 3 k 4 jk 3 nm 4 nm 2 output k 4 jk 3 thanks (7 Replies)
Discussion started by: phil_heath
7 Replies

10. Shell Programming and Scripting

delete a string on column1

Hi I have a file with multiple columns. But there is something weird on column one that is attached to the name. The good thing is that its a consistent pattern so there should be a way to remove it. So the first column looks something like this: name_345.4ml date_3456.4ml year_12.4ml... (3 Replies)
Discussion started by: kylle345
3 Replies
Login or Register to Ask a Question

Featured Tech Videos