search for a "string" in a file

I have a file with 4 coulms, i am suppose write a code to search for a string only in one column and once i find the $string, i have to print other colums in the matched row. I am using "cut -ccolumn FILENAME | grep -i $string". But, the problem is once i find the STRING, I need to print other coulmns that user wants me to print in the matched $string row. I can't get this to figure out. If I redirect my output to other file with line number, how can go back to my original file and get the appropiate columns for my string. Pls help me with this. Thanx to u all.

As u can tell i am a NEWBIE

eq.

My file that i am searching in.
1234567890123456789012
John martin 1234 56786
your name John 12374

if i am searching for JOHN it should only search from column 1-11. and print other output from that row either 1234 or 56786. It shouldn't search JOHN from row2.

Thank you all

Comments

  • : I have a file with 4 coulms, i am suppose write a code to search for a string only in one column and once i find the $string, i have to print other colums in the matched row. I am using "cut -ccolumn FILENAME | grep -i $string". But, the problem is once i find the STRING, I need to print other coulmns that user wants me to print in the matched $string row. I can't get this to figure out. If I redirect my output to other file with line number, how can go back to my original file and get the appropiate columns for my string. Pls help me with this. Thanx to u all.
    :
    : As u can tell i am a NEWBIE
    :
    : eq.
    :
    : My file that i am searching in.
    : 1234567890123456789012
    : John martin 1234 56786
    : your name John 12374
    :
    : if i am searching for JOHN it should only search from column 1-11. and print other output from that row either 1234 or 56786. It shouldn't search JOHN from row2.
    :
    : Thank you all
    :

    Hi,

    You can try to solve the problem with awk. If You use a statement like this:
    awk '$1 ~ /JOHN/ { print $3" "$4 }' myfile
    it will search for JOHN in the first column and prints the fields 3 and 4.
    If You can afford it, then try the book "sed & awk" from OReilly.
    Also, for short help, try this link: torvalds.cs.mtsu.edu/~neal/awkcard.pdf

    Hope that helped.

    regards

    Thorsten
  • : : I have a file with 4 coulms, i am suppose write a code to search for a string only in one column and once i find the $string, i have to print other colums in the matched row. I am using "cut -ccolumn FILENAME | grep -i $string". But, the problem is once i find the STRING, I need to print other coulmns that user wants me to print in the matched $string row. I can't get this to figure out. If I redirect my output to other file with line number, how can go back to my original file and get the appropiate columns for my string. Pls help me with this. Thanx to u all.
    : :
    : : As u can tell i am a NEWBIE
    : :
    : : eq.
    : :
    : : My file that i am searching in.
    : : 1234567890123456789012
    : : John martin 1234 56786
    : : your name John 12374
    : :
    : : if i am searching for JOHN it should only search from column 1-11. and print other output from that row either 1234 or 56786. It shouldn't search JOHN from row2.
    : :
    : : Thank you all
    : :
    :
    : Hi,
    :
    : You can try to solve the problem with awk. If You use a statement like this:
    : awk '$1 ~ /JOHN/ { print $3" "$4 }' myfile
    : it will search for JOHN in the first column and prints the fields 3 and 4.
    : If You can afford it, then try the book "sed & awk" from OReilly.
    : Also, for short help, try this link: torvalds.cs.mtsu.edu/~neal/awkcard.pdf
    :
    : Hope that helped.
    :
    : regards
    :
    : Thorsten
    :

    Thanx Thorsten for replying, But AWK statement wouldn't work for me bcuz if only looks from a Field not column. And plus i cannot search in 2 fields at a time.

    This is exactly my datafile looks like....
    1-11, department- 12-24, Name - 28-39 add -40-44 salary
    i have to search in a NAME COLUMN...mean I have to search John in last name and first name
    if user types ohn....it should match John and Johnson, but it should only search in NAME colum.

    12345678901234567890123456789012345678901234
    EngineeringJohn Jacobs 123 Frank Blvd. 1234
    Businees Eric Johnson234 bob street. 1234
    IT Julie Gibson456 John street.1234

    Thanx,

    I really need help with this sum 1 pls help
  • : : : I have a file with 4 coulms, i am suppose write a code to search for a string only in one column and once i find the $string, i have to print other colums in the matched row. I am using "cut -ccolumn FILENAME | grep -i $string". But, the problem is once i find the STRING, I need to print other coulmns that user wants me to print in the matched $string row. I can't get this to figure out. If I redirect my output to other file with line number, how can go back to my original file and get the appropiate columns for my string. Pls help me with this. Thanx to u all.
    : : :
    : : : As u can tell i am a NEWBIE
    : : :
    : : : eq.
    : : :
    : : : My file that i am searching in.
    : : : 1234567890123456789012
    : : : John martin 1234 56786
    : : : your name John 12374
    : : :
    : : : if i am searching for JOHN it should only search from column 1-11. and print other output from that row either 1234 or 56786. It shouldn't search JOHN from row2.
    : : :
    : : : Thank you all
    : : :
    : :
    : : Hi,
    : :
    : : You can try to solve the problem with awk. If You use a statement like this:
    : : awk '$1 ~ /JOHN/ { print $3" "$4 }' myfile
    : : it will search for JOHN in the first column and prints the fields 3 and 4.
    : : If You can afford it, then try the book "sed & awk" from OReilly.
    : : Also, for short help, try this link: torvalds.cs.mtsu.edu/~neal/awkcard.pdf
    : :
    : : Hope that helped.
    : :
    : : regards
    : :
    : : Thorsten
    : :
    :
    : Thanx Thorsten for replying, But AWK statement wouldn't work for me bcuz if only looks from a Field not column. And plus i cannot search in 2 fields at a time.
    :
    : This is exactly my datafile looks like....
    : 1-11, department- 12-24, Name - 28-39 add -40-44 salary
    : i have to search in a NAME COLUMN...mean I have to search John in last name and first name
    : if user types ohn....it should match John and Johnson, but it should only search in NAME colum.
    :
    : 12345678901234567890123456789012345678901234
    : EngineeringJohn Jacobs 123 Frank Blvd. 1234
    : Businees Eric Johnson234 bob street. 1234
    : IT Julie Gibson456 John street.1234
    :
    : Thanx,
    :
    : I really need help with this sum 1 pls help
    : Sorry when i post my data file. It removes all the extra spaces between two coulmns.

  • cat $file | while read line #Reading line by line of the file
    do
    #The following IF looks for a string in a NAME COLUMN of current line
    if echo "$line" | cut -c12-27 | grep -i "$employee_name" >/dev/null ; then
    #The following Case cheks for options that user entered and outputs #accordingly
    case $option in
    a|A)
    echo "$line" | cut -c12-27,28-59;;
    s|S)
    echo "$line" | cut -c12-27,60-65;;
    *)
    echo "$line" | cut -c12-27;;
    esac fi done

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories