How to search for a word in a particular column of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search for a word in a particular column of a file
# 1  
Old 12-29-2010
How to search for a word in a particular column of a file

How to search for a word like "computer" in a column (eg: 4th field) of a '***' delimited file and add a column at the end of the record which denotes 'Y' if present and 'N' if not. After this, we need to again check for words like 'Dell' but not 'DellXPS' in 5th field and again add another column at the end of the record with 'Y' or 'N'. The Input text file may contain 2500 lines, so it should append the 2 new columns at the end of each line.
Please help!

so, input text file like:
Code:
0001***abcd***fghi***computer of the year***DellXPS or Toshiba or Sony

and output should be:
Code:
0001***abcd***fghi***computer of the year***DellXPS or Toshiba or Sony***Y***N


Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples!

Last edited by Franklin52; 12-29-2010 at 09:24 AM..
# 2  
Old 12-29-2010
Code:
cat inputFile | tr '***' '|' | while read line
do
    forthField=`echo "$line" | cut -d"|" -f4`
    fifthField=`echo "$line" | cut -d"|" -f5`

    if `echo "$forthField" | grep -i computer` ; then
        line="${line}|Y"
    else
        line="${line}|N"
    fi

    if `echo "$fifthField" | grep -i "Dell "` ; then
        line="${line}|Y"
    else
        line="${line}|N"
    fi
    
    echo "$line" | sed -n 's/\|/***/g' >> outputFile
done

This User Gave Thanks to For This Post:
R0H0N
# 3  
Old 12-29-2010
Try this,

Code:
awk -F"***" 'BEGIN{OFS=FS}{print $0,$4 ~ /computer/?$6="Y":$6="N",$5=="Dell"?$7="Y":$7="N"}' inputfile

# 4  
Old 12-29-2010
Code:
awk -F '***' '{print $0,$4~/computer/?"Y":"N",$5~/(^|[ \t])Dell([ \t]|$)/?"Y":"N"}' OFS='***' file

# 5  
Old 12-29-2010
Code:
awk 'BEGIN{FS=OFS="***"}{$6=($4~/computer/)?"Y":"N";$7=($5~/Dell/&&$5!~/DellXPS/)?"Y":"N"}1' file

# 6  
Old 12-30-2010
Thanks Rohon. we made the changes in tr and used awk to use the same delimiter itself. Also IF condition needed a ". Code for only the first condition was:

cat inputFile | while read line
do
forthField=`echo "$line" | awk -F "***" '{print $4}`

if [ "`echo "$forthField" | grep -i "computer"`" ] ; then
line="${line}@@@Y"
else
line="${line}@@@N"
fi

echo "$line" >> outputFile
done


It worked fine! Thanks all.

Last edited by Jassz; 12-30-2010 at 01:26 AM.. Reason: line mismatch
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. Shell Programming and Scripting

Search for a word in file and output to another

Hello Experts! I am trying to perform a common task, searching data from within a file and placing what is found into another.However, I have not been able to figure the “How to” with this situation. I need to search through all lines within this text and pull the first positional attributes... (5 Replies)
Discussion started by: leepet
5 Replies

3. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

4. Shell Programming and Scripting

How to search a word in a file?

How to search a particular word in a file. in my file contains lacks of rows. (2 Replies)
Discussion started by: pmreddy
2 Replies

5. UNIX for Dummies Questions & Answers

Search word in 3rd column and move it to next column (4th)

Hi, I have a file with +/- 13000 lines and 4 column. I need to search the 3rd column for a word that begins with "SAP-" and move/skip it to the next column (4th). Because the 3rd column need to stay empty. Thanks in advance.:) 89653 36891 OTR-60 SAP-2 89653 36892 OTR-10 SAP-2... (2 Replies)
Discussion started by: AK47
2 Replies

6. Shell Programming and Scripting

search a word and copy the file

Hi need help with a script or command My requirement is - I need to do a "ls -ltr tcserver*.syslog" files in /tmp, direct the output to a file ls -ltr tcserv*.syslog | grep "Jan 31" | awk '{printf "\n" $9}' > jandat.logs -Then open each file in above list and search for string/word,... (4 Replies)
Discussion started by: karghum
4 Replies

7. Shell Programming and Scripting

To search a word in particular column using awk

I have a data in a file like this 1 praveen bmscollege 2 shishira bnmit 3 parthiva geethamce I want to search "praveen" using awk command i tried like this but i did not get awk `$2="praveen" {print $0} ` praveen.lst can anyone help me solving this problem in... (2 Replies)
Discussion started by: praveenhegde
2 Replies

8. Shell Programming and Scripting

Search for last instance of word in a file

Hi I'm trying to search for the last instance of the word 'cache' in a HTML file that I have downloaded from YouTube. I'm using the following syntax, but an error is thrown when I try it. grep -f "cache" Also I wish to append the above grep command to the below so that the search for cache... (3 Replies)
Discussion started by: colmbell
3 Replies

9. UNIX for Dummies Questions & Answers

Search a specific word from any one of the file.

how do i Search a specific word from any one of the file.? (1 Reply)
Discussion started by: ritusubash
1 Replies

10. Shell Programming and Scripting

search a word from file

Hello, I have a file which contains some SQL statements. I need to take out the table name from the file. Table name will always end with "_t". can anyone help me in getting that? for e.g. --- SQL_STMT do_sql_insert: cmd="insert into account_t ( poid_DB, poid_ID0, poid_TYPE, poid_REV,... (9 Replies)
Discussion started by: vishy
9 Replies
Login or Register to Ask a Question