Changed Field Number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changed Field Number
# 1  
Old 06-26-2013
Changed Field Number

HI,
I have two files and contains many Fields or columns with | (pipe) delimitor, wanted to compare both the files and get only unmatched perticular fields number.
ex:
first.txt
Code:
 1  |  2 | 3
111 |abc| 230 
hbc | bb2 | cs

second.txt
Code:
1  | 2   | 3
111 |abc |230 
abn | bb2 | fp

Here the different data in two fileds(i.e abn, fp in second file), the thing is i want only those changed fields or column numbers
The same scipt will be used for many files in my project so the field changes are happens any filed in the second file, so please provide the scipt or command with accurate result.
output.txt
Code:
1
3

Thanks.

Last edited by Franklin52; 06-26-2013 at 04:01 AM.. Reason: Code tags
# 2  
Old 06-26-2013
so what have you tried so far?

btw, it seems like you recently have been asking the same questions repeatedly in some form or another. if so, maybe you need to understand the answers to the other threads first before asking the same question again.
These 2 Users Gave Thanks to Just Ice For This Post:
# 3  
Old 06-26-2013
Sorry i was trying many times previous method but i could not succeeded please provide another solution for the above thread.

Kindly respond ...

---------- Post updated at 06:18 AM ---------- Previous update was at 05:53 AM ----------

Code:
awk 'NR==1 {n=split($0,a," ")} NR==2 {split($0,b," ")} END {for (i=0;i++ <=n-1;) {if (a[i]!=b[i]) {print i;exit}}}' infile


This will compare only single file, but i wnated to compare two files and get unmatched field number in second file as mentioned above thread...please help thanks.

Last edited by Scrutinizer; 06-26-2013 at 06:46 PM.. Reason: code tags
# 4  
Old 06-26-2013
What be
Quote:
1
3
? Does it mean fields 1 and 3 in row three? What if non-matches occur in other lines?

BTW - at least half the fields in your samples do not match.

Last edited by RudiC; 06-26-2013 at 07:34 PM.. Reason: made comment more specific
# 5  
Old 06-26-2013
Try:

Code:
awk -F\| '
  {
    split($0,F)
    getline<f
    for(i=1; i<=NF;i++) if($i!=F[i]) C[i]
  } 
  END{
    for(i in C) print i
  }
' f=file1 file2

are there really that many random spaces in your actual input file? If so try:
Code:
awk -F\| '
  {
    gsub(/[ \t]/,x)
    split($0,F)
    getline<f
    gsub(/[ \t]/,x)
    for(i=1; i<=NF;i++) if($i!=F[i]) C[i]
  } 
  END{
    for(i in C) print i
  }
' f=file1 file2

# 6  
Old 06-26-2013
@Scrutinizer: What if the same field no. doesn't match in several lines? It would print the field no. just once, wouldn't it? Not sure if this be the desired behaviour...
# 7  
Old 06-26-2013
As has already been noted, you have started multiple threads that seem to be asking the same question. When you are asked to clarify what your are trying to do, you open a new thread instead of answering questions that might enable us to figure out what you are trying to do.

In this thread you showed us first.txt and second.txt that both have 3 lines with three fields separated by "|" on each line. The first two fields of the first line of these files are different, the last two fields of the second line are different, and the first and third fields of the last line are different. Then you say that the result you want is:
Code:
1
3

We do not understand how six fields out of nine being different between your two input files results in the two lines of output you say you want!!!

If you want our help, please give a clear statement of what you are trying to do and a clear description of the output you are trying to produce.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

2. Shell Programming and Scripting

Get the changed field number

HI, I have two files and contains many Fields or columns with | (pipe) delimitor, wanted to compare both the files and get only unmatched perticular fields number. ex: first.txt 111 |abc| 230| hbc | bb2 | cs second.txt 111 |abc |230 |abn | bb2 | fp Here the different data in two... (2 Replies)
Discussion started by: prawinmca
2 Replies

3. Shell Programming and Scripting

Script to echo "File permissions or ownership changed from required " when accidentally changed.

Hi All, I have to work in the late nights some times for server maintenance and in a hurry to complete I am accidentally changing ownership or permission of directories :( which have similar names ( /var in root and var of some other directory ).:confused: Can some one suggest me with the... (1 Reply)
Discussion started by: shiek.kaleem
1 Replies

4. Shell Programming and Scripting

csv file field needs to be changed current system date with awk

HI, I have csv file with records as shown below. 4102,Bangalore,G10,21,08/17/2011 09:28:33:188,99,08/17/2011 09:27:33:881,08/17/2011... (1 Reply)
Discussion started by: raghavendra.nsn
1 Replies

5. Shell Programming and Scripting

Adding total of first field for each number in the second field

Dears, I need a script or command which can find the unique number from the second filed and against that number it adds the total of first field . 17215630 , 0 907043 ,1 201050 ,10 394149 ,4 1964 ,9 17215630, 0 907043 ,1 201050, 10 394149 ,4 1964 ,9 1234234, 55 23 ,100 33 ,67 ... (2 Replies)
Discussion started by: shary
2 Replies

6. Shell Programming and Scripting

Count number of occurences of a character in a field defined by the character in another field

Hello, I have a text file with n lines in the following format (9 column fields): Example: contig00012 149606 G C 49 68 60 18 c$cccccacccccccccc^c I need to count the number of lower-case and upper-case occurences in column 9, respectively, of the... (3 Replies)
Discussion started by: s052866
3 Replies

7. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

8. Shell Programming and Scripting

Diff Command to return the number of lines inserted,deleted and changed.

Hello, I have to compare two files file1 and file2, retrieve the number of lines modified (added, deleted or modified) in file2. Output must be like: File2: Added Deleted Changed Total ------ ------- -------- ----- 2 1 1 4 Somebody... (2 Replies)
Discussion started by: nmattam
2 Replies

9. Shell Programming and Scripting

How to print empty line when the a field is changed

Hi all, I have this input file .. BSS01 107 Swafieh 11/06/2008 12:06:57 BSS01 111 Ramada_Hotel 12/06/2008 11:37:20 BSS01 147 Kalha_Rep 11/06/2008 19:13:39 BSS01 147 Kalha_Rep ... (9 Replies)
Discussion started by: yahyaaa
9 Replies
Login or Register to Ask a Question

Featured Tech Videos