Diference of file in a line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Diference of file in a line
# 1  
Old 06-12-2007
Diference of file in a line

I am going to arrange a data and excel can't do this functionality.
If anyone here could help me do this i will really appreicate it.

I have a file that contains thousands columns and it looks like this

User1 SF SMB QH
user2 SF SF
User3 QH
User4 SMB QH

I need to extract the users with difference entries on columns 1-etc only so the output should look like this.

User1
User4
# 2  
Old 06-12-2007
Kharen,
Based on your definition, the output should be:
Code:
User1
User3
User4

Since all three users do not have any duplicate in its line.

In any event, see if this solution works for you:
Code:
typeset -i mCnt
typeset -i mTotOcc
while read mLine
do
  set -- `echo $mLine | sed 's/,/ /g'`
  mUser=$1
  shift
  mTotal=${#}
  mLineNoUser=`echo $mLine | sed "s/$mUser//"`
  mFlag=""
  mCnt=1
  while [ $mCnt -le ${mTotal} ]; do
    mField=$1
    mTotOcc=`echo "$mLineNoUser" | sed "s/$mField//" | egrep -c "$mField"`
    if [ $mTotOcc -gt 0 ]; then
      mFlag="DUPLICATE"
      break
    fi
    mCnt=$mCnt+1
    shift
  done
  if [ "$mFlag" = "" ]; then
    echo "User = "$mUser" has no duplicate."
  fi
done < input_file

# 3  
Old 06-12-2007
Quote:
Originally Posted by Shell_Life
Kharen,
Based on your definition, the output should be:
Code:
User1
User3
User4

Each user should have same inputs per column, otherwise, output the user on a file.

The output is only User1 and User4 since these users have different inputs
# 4  
Old 06-12-2007
Kharen,
I still do not understand your specification.
More examples and detailed requirements are needed.
# 5  
Old 06-12-2007
Quote:
Originally Posted by Shell_Life
Kharen,
I still do not understand your specification.
More examples and detailed requirements are needed.
on my example:
User1 SF SMB QH
user2 SF SF
User3 QH
User4 SMB QH
User5 QH QH QH

On the above example the result should be User1 and User4 Because User 2 has both SF's and User3 got only one entry (no one to compare to) and User5 has both QH's.

That leaves us to User1 and User 4 for the result.

I'm sure its clearer this time Smilie
# 6  
Old 06-12-2007
Code:
#!/bin/sh

while read line ; do
        [ -z `echo $line | awk '{print $3}'` ] && continue
        [ -z `echo $line | tr ' ' '\n' | uniq -d` ] && echo $line | awk '{print $1}'
done < "$1"

Run this script as:
$ script file
# 7  
Old 06-12-2007
Code:
typeset -i mCnt
typeset -i mTotOcc
while read mLine
do
  set -- `echo $mLine | sed 's/,/ /g'`
  mUser=$1
  shift
  mFlag=""
  mTotal=${#}
  if [ $mTotal -gt 1 ]; then
    mLineNoUser=`echo $mLine | sed "s/$mUser//"`
    mCnt=1
    while [ $mCnt -le ${mTotal} ]; do
      mField=$1
      mTotOcc=`echo "$mLineNoUser" | sed "s/$mField//" | egrep -c "$mField"`
      if [ $mTotOcc -gt 0 ]; then
        mFlag="DUPLICATE"
        break
      fi
      mCnt=$mCnt+1
      shift
    done
    if [ "$mFlag" = "" ]; then
      echo "User = "$mUser" has no duplicate."
    fi
  fi
done < input_file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

2. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

3. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

4. Shell Programming and Scripting

Match a line in File 1 with Column in File 2 and print whole line in file 2 when matched

Hi Experts, I am very new to scripting and have a prb since few days and it is urgent to solve so much appreciated if u help me. i have 2 files file1.txt 9647810043118 9647810043126 9647810043155 9647810043161 9647810043166 9647810043185 9647810043200 9647810043203 9647810043250... (22 Replies)
Discussion started by: mustafa.abdulsa
22 Replies

5. Shell Programming and Scripting

What is the diference between text files?

Hello World! :) I did one shell script to read one txt file containing one phone numbers list and perform one search of numbers into a couple of log files. When I use the file called "qq.txt" the script works correctly but using file called "MSISDN_Deactivation_Pending_Todos.txt" numers... (3 Replies)
Discussion started by: orma
3 Replies

6. Shell Programming and Scripting

Read file line by line and process the line to generate another file

Hi, i have file which contains data as below(Only sample shown, it may contain more data similar to the one shown here) i need to read this file line by line and generate an output file like the one below i.e based on N value the number of MSISDNs will vary, if N=1 then the following... (14 Replies)
Discussion started by: aemunathan
14 Replies

7. Solaris

Solaris container date diference

Hi people, I dont have a real problem, its also a so strange issue. When i connect to my system from a ssh session or telnet normaly from putty and execute the command "date" its show me the correctly time in BRT format for root and all other users. But when i connect from the global... (3 Replies)
Discussion started by: anonymouzz
3 Replies

8. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

9. Shell Programming and Scripting

shell script to read a line in gps receiver log file and append that line to new file

Hi, I have gps receiver log..its giving readings .like below Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GPSD,R=1 $GPGSV,3,1,11,08,16,328,40,11,36,127,00,28,33,283,39,20,11,165,00*71... (3 Replies)
Discussion started by: gudivada213
3 Replies
Login or Register to Ask a Question