how to print the column that contains a string in delimited file in unix


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to print the column that contains a string in delimited file in unix
# 1  
Old 04-04-2012
how to print the column that contains a string in delimited file in unix

Hi Team,

I have a requirement to print the column or find the column number of a particular string in delimited (Smilie file.

Ex:

Code:
aaa;bbb;ccc;rrr;mmm;
gggg;rrr;mmmm;ssss;eee;aaa;
ccc;gggg;tttt;bbbb;dddd;ggggg;rrr;

my file contains 1000+ rows like above example with semicolon delimited. i need to take string 'rrr' column in each row.



Please help me on this.

thanks,
Baski

Last edited by Scrutinizer; 04-04-2012 at 03:57 AM.. Reason: Code tags...
# 2  
Old 04-04-2012
Code:
awk -F";" '{for(i=1;i<=NF;i++){if ($i ~ /rrr/){print i}}}' filename

# 3  
Old 04-04-2012
Hi ningy,
small change in your solution

Code:
awk -F";" '{for(i=1;i<=NF;i++){if ($i ~ /rrr/){print $i}}}' FileName

Regards,
Shanmu

---------- Post updated at 11:33 AM ---------- Previous update was at 11:31 AM ----------

Hi baskivs,

Try this also,

Code:
sed 's/\(.*\)\;\(rrr\)\;\(.*\)/\2/g' FileName

Regards,
Shanmu
# 4  
Old 04-04-2012
Hi ningy,
small change in your solution

Code:
awk -F";" '{for(i=1;i<=NF;i++){if ($i ~ /rrr/){print $i}}}' FileName

Regards,
Shanmu

---------- Post updated at 11:33 AM ---------- Previous update was at 11:31 AM ----------

your change is wroong if u use $i it will print rrr..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

2. UNIX for Beginners Questions & Answers

UNIX - 2 tab delimited files, conditional column extraction

Please know that I am very new to unix and trying to learn 'on the job'. I'm only manipulating large tab-delimited files (millions of rows), but I'm stuck and don't know how to proceed with the following. Hoping for some friendly advice :) I have 2 tab-delimited files - with differing column &... (10 Replies)
Discussion started by: GTed
10 Replies

3. Shell Programming and Scripting

Replace pipe delimited column string to null

Hi All, I have a large dat file where each lines are pipe delimited values. I need to parse the file depending on the request. For example: sometimes I have told to remove all the values in the 7th column (this case remove values '3333' only from the first line and '3543' from the second line)... (4 Replies)
Discussion started by: express14
4 Replies

4. Shell Programming and Scripting

Replace specific column range in a non-delimited file with a string!

Hi All, I will need an help with respect to replacing a range of columns on a non-delimited file using a particular string pattern. Say file input is MYNUMBERD000000-BAN CHUE INSNTS ** N+ MYAREDSDD000000+BAN CHUE INSNTS ** N+ MYDERFFFSD00000-GIR PENT - ACH ** ... (5 Replies)
Discussion started by: navojit dutta
5 Replies

5. UNIX for Advanced & Expert Users

Parse (delimited string) key-value pairs in a column into separate lines

Hi experts, e.g. i/p data looks like 0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747,9700005405717924,9700005405733788|unidentified,unidentified,unidentified|| o/p data should like - row1: 0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747|unidentified ... (1 Reply)
Discussion started by: sumoka
1 Replies

6. UNIX for Dummies Questions & Answers

Search and replace string only in a particular column in a delimited file

I have file with multiple columns. Column values for a record may be same. Now i have to replace a column value(this can be same for the other columns) with new value. File.txt A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D... (1 Reply)
Discussion started by: ksailesh
1 Replies

7. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

8. Shell Programming and Scripting

Delete parts of a string of character in one given column of a tab delimited file

I would like to remove characters from column 7 so that from an input file looking like this: >HWI-EAS422_12:4:1:69:89 GGTTTAAATATTGCACAAAAGGTATAGAGCGT U0 1 0 0 ref_chr8.fa 6527777 F DD I get something like that in an output file: ... (13 Replies)
Discussion started by: matlavmac
13 Replies

9. UNIX for Dummies Questions & Answers

Trim String in 3rd Column in Tab Delimited File...SED/PERL/AWK?

Hey Everybody, I am having much trouble figuring this out, as I am not really a programmer..:mad: Datafile.txt Column0 Column1 Column2 ABC DEF xxxGHI I am running using WGET on a cronjob to grab a datafile, but I need to cut the first three characters from... (6 Replies)
Discussion started by: rickdini
6 Replies

10. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies
Login or Register to Ask a Question