Can SED Search By Column?

Hi all,

I am trying to search for a keyword in the fourth column of a massive carrot(^) delimited file and unfortunately I cannot use AWK (which would have been ideal). Can SED (or maybe even GREP) perform a search like this?

are you ready to go with the combination of cut and sed command??

I'm open to any suggestions at this point :slight_smile:

then this will do..

Thanks, but there is no replacement pattern in my case. I just need it to return the complete line that matches the search pattern to me. I tried modifying your example by omitting the "replacepattern" argument but that gave me an error that said it couldn't be parsed. :frowning:

for that use this..

Thanks for working with me on this problem. I need to ask your help with one more correction. This command you suggested outputs only the search pattern when found. How can I make it output the entire line where the search pattern was found?

Example: If I'm searching for "SMITH" then cut -f4 -d \^ filename | sed -ne '/SMITH/p' returns:
SMITH
SMITH

I need it to return:
data1^adbde^1981^SMITH
ender2^23232^1972^SMITH

thats very difficult without awk command.. will try..

Thanks. Much appreciated.

this looks bit ugly but it works .. :slight_smile:


  1. ^\^ ↩︎

  2. ^\^ ↩︎

  3. ^\^ ↩︎

That works great! Thanks. I thought I was really familiar with grep but I have to admit you've stumped me on this one. Can you break down the logic for me so that I know for next time?


  1. ^\^ ↩︎

  2. ^\^ ↩︎

  3. ^\^ ↩︎

logic is quite simple..
here i am escaping the first three char^char^char^pattern and the searching in 4th field that means it just ignores whatever is first three field..
hard to explain.. :stuck_out_tongue:

The solution below is not bullet proof, just gives you some ideas

$ cat xx
dfhgj1^adbde^1981^SMITH^qwe^asd
ender2^23232^1972^1234^QWE^ASD
adfgh3^cvbdg^1981^SMITH^qwe^asd
zxcvb4^#####^1972^09876^QWE^ASD

$ a.sh xx
dfhgj1 adbde 1981 SMITH qwe asd
adfgh3 cvbdg 1981 SMITH qwe asd

$ cat a.sh

#!/bin/ksh
cat $1 | while read src_line
do 
        IFS2=$IFS; 
        IFS='^';
        set - $src_line; 
        # now you have all fields in $1, $2, ...
        x=$( echo $4 | sed -ne '/SMITH/p' )
        # if sed found your pattern, spit it out
        if [[ -n $x ]]; then
                echo $src_line;
        fi
        IFS=$IFS2;
done