02-25-2008
Text file manipulation
I am a new unix user & I wanted to work with unix as it is very good in text manipulations. I need a little help. I will be grateful if someone can help me out.
I need help in grepping a pattern of numbers from one file to another file. Specific details are as follows:
File one contains only one column of numbers like
897887738753
881388745527
237818494899
331388745702
451388746000
911388746146
891388746156
file 2 contains three column of numbers 1st column is just the hash mark then another set of numbers & then column 3 (the numbers of my interest).
## 234451388746000 447887738753
## 234159018106564 447818494899
## 234159055332992 897887738753
## 234159043784408 881388745527
## 234159026266547 237818494899
## 234154821274370 331388745702
## 234159053081262 451388746000
## 234159020233321 911388746146
## 234159014081879 891388746156
I want to grep numbers in File 1 from 3rd column of file 2 and as a result I should get out put as the whole line in file 2 which has the matching number in column 3 of file 1
I am trying: grep -f file1 file2 > file3
but the problem with it is that if it finds a similar match even in column 2 it prints it. Like in above case output will be
## 234159055332992 897887738753
## 234159043784408 881388745527
## 234159026266547 237818494899
## 234154821274370 331388745702
## 234159053081262 451388746000
## 234159020233321 911388746146
## 234159014081879 891388746156
&
## 234451388746000 447887738753 (which is not right because the 3rd column of file2 does not have any match in file1)
One more thing I am using cygwin to create unix enviornment on my windows machine & its not actually a unix machine but just unix environment.
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
Hi,
I need to remove lines from a text file that are less than certain length in UNIX. For example, test.txt file contains the following lines:
abcdefghijklmnopqrstuvwxyz.
123456789009876543211234567
This line to be removed.
zyxwvutsrqponmlkjihgfedcba.
The length of each line is supposed... (5 Replies)
Discussion started by: svannala
5 Replies
2. Shell Programming and Scripting
Hello,
I have mysql binary file which logs all the database queries and i to insert all queries log in to database. First i coverted binary file to text file. and start playing with it. Text file contains following queries, some samples are,
SET INSERT_ID=1;
INSERT INTO test... (0 Replies)
Discussion started by: mirfan
0 Replies
3. Shell Programming and Scripting
What I am trying to do is make a script that will add a port number within a section of a file if it already doesn't exist in that section of the file. The particular line that I would like to add the port number to in the file is formatted like this:
TCPPORTS="25 80 125 443 8080 10000"
For... (3 Replies)
Discussion started by: nullifx
3 Replies
4. Shell Programming and Scripting
Hi,
I need to write shell script for the scenario explained below -
datafile.txt
AcctNum,code,Region,,,,
12345451,AN ,abaab
12345452,AN ,xccxc
76677545,RP ,acxcc
43567878,RP ,afghh
32190900,AN ,afrfrf
87312345,AN ,aqaw
I have a text file (datafile.txt)... (1 Reply)
Discussion started by: ravigupta2u
1 Replies
5. UNIX for Dummies Questions & Answers
Hi I've two text files FILE_1 and FILE_2 as shown below:
FILE_1.txt
CO Contig1 342 12 11 U
GGGCTGACGTGGCCGCTAATACGACTCACTATAGGG*AGAGAAGTCATTTTCTTGTTTAG
BQ
35 35 35 50 50 50 50 50 50 50 50 60 65 65 65 65 65 65 65 65 65 65 65 65 50
AF GP5UOVN01AOPE0 U 1
AF GP5UOVN01AT8W3 U 1
... (1 Reply)
Discussion started by: Fahmida
1 Replies
6. Shell Programming and Scripting
Hi ,
I have a simple text file with contents as below:
12345678900 971,76 4234560890
22345678900 5971,72 5234560990
32345678900 71,12 6234560190
the new csv-file should be like:
Column1;Column2;Column3;Column4;Column5
123456;78900;971,76;423456;0890... (9 Replies)
Discussion started by: FreddyDaKing
9 Replies
7. Programming
Hi
I have a requirement to write a script but not sure which is the best way to approach this
I have not worked in sed but I'm aware that its robust for file extraction requirements
I have a scripting task. I already developed the code in perl but the script is taking almost 2 mins for... (5 Replies)
Discussion started by: John Trevor
5 Replies
8. UNIX for Dummies Questions & Answers
I have a tab delimited file with 4 columns. If the value in the first column, equals the value in the second column, I'd like to have the 4th column multiplied by 2 then add 1. If the value in the first column differs from the value in the second, I'd like to have the 4th column multiplied by 2... (5 Replies)
Discussion started by: evelibertine
5 Replies
9. Shell Programming and Scripting
Hi Gurus,
I have a question
I have a flat file like below with three fields (3 rd field is amt)
ad|B|500
cc||100
dd|C|600
ee||900
Need to write a code in such a way that when second field is empty then do sum of third field
So in this case it will be 100 +900
I tried but no luck... (1 Reply)
Discussion started by: patricjemmy6
1 Replies
10. UNIX for Dummies Questions & Answers
Hello,
Supposing I had a huge list as follows:
TAC manufacturer Device Type
1392600 LG D959 LG-D959TS FeaturePhone
1409700 LG V410 FeaturePhone
35150806 LG F350S FeaturePhone
35165206 Samsung GT-E1200 FeaturePhone
35194505 Nokia Asha 200 FeaturePhone
but I want to make it look like... (3 Replies)
Discussion started by: Cludgie
3 Replies
comm(1) General Commands Manual comm(1)
NAME
comm - select or reject lines common to two sorted files
SYNOPSIS
file1 file2
DESCRIPTION
comm reads file1 and file2, which should be ordered in increasing collating sequence (see sort(1) and Environment Variables below), and
produces a three-column output:
Column 1: Lines that appear only in file1,
Column 2: Lines that appear only in file2,
Column 3: Lines that appear in both files.
If is used for file1 or file2, the standard input is used.
Options 1, 2, or 3 suppress printing of the corresponding column. Thus prints only the lines common to the two files; prints only lines in
the first file but not in the second; does nothing useful.
EXTERNAL INFLUENCES
Environment Variables
determines the collating sequence expects from the input files.
determines the language in which messages are displayed.
If is not specified in the environment or is set to the empty string, the value of determines the language in which messages are displayed.
If is not specified in the environment or is set to the empty string, the value of is used as a default. If is not specified or is set to
the empty string, a default of ``C'' (see lang(5)) is used instead of If any internationalization variable contains an invalid setting,
behaves as if all internationalization variables are set to ``C''. See environ(5).
International Code Set Support
Single- and multi-byte character code sets are supported.
EXAMPLES
The following examples assume that and have been ordered in the collating sequence defined by the or environment variable.
Print all lines common to and (in other words, print column 3):
Print all lines that appear in but not in (in other words, print column 1):
Print all lines that appear in but not in (in other words, print column 2):
SEE ALSO
cmp(1), diff(1), sdiff(1), sort(1), uniq(1).
STANDARDS CONFORMANCE
comm(1)