Results 1 to 5 of 5

Thread: match a line when i have values from that line

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default match a line when i have values from that line

    hello. can someone plz help me find a way to match a line from a file that has text fields in it. i know the values of the text fields but the problem is that there is often random amounts of spaces between them so i never know how to search it properly, if i just put echo -e with \t it doesnt find anything. anyone??

  2. #2
    Senior Member
    Join Date
    Aug 2011
    Posts
    448
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    hi,

    Code:
    [[:blank:]]*
    will match any number of space or tab
    see man 7 regex.
    A problem clearly stated is a problem half solved.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by Watael View Post
    hi,

    Code:
    [[:blank:]]*
    will match any number of space or tab
    see man 7 regex.
    Thank you!
    I also have another problem.
    I have a file say called config and it has 4 fields: domain, type, item, and value. If the user enters the 3 first fields and they match, but the last field (value) is different from the one in the file, then the the value gets substitute by what was entered by the value entered by the user. I tried the following sript but it doesnt work.

    #!/bin/bash
    domain=$1
    type=$2
    item=$3
    value=$4

    a=$(awk -vdomain=$1 -vtype=$2 -vitem=$3 -vvalue=$4 '$1=="domain" && $2=="type" && $3=="item"' config)
    b=$(echo $a | sed -r 's/\t/ /g' | cut -d" " -f4)
    echo $a | awk '{sub("$b","$4"); print $0}'



    config file is like this with tabs

    misha hard nice 2
    domain soft cake 5

  4. #4
    Senior Member
    Join Date
    Aug 2011
    Posts
    448
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    I'd simply, and systematically, replace any lines having first three fields which match:
    Code:
    sed "s/$1\t$2\t$3\t/&$4" UrFile
    even fourth field matches.
    A problem clearly stated is a problem half solved.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Minor detail:
    sed command is missing a trailing / delimiter.
    Would crash on GNU sed, probably most other sed versions.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. cshell reading file line by line
    By eawedat in forum Shell scripting
    Replies: 0
    Last Post: 25th August 2012, 12:06 AM
  2. sed: delete line in file and line above
    By mjrummel in forum Shell scripting
    Replies: 4
    Last Post: 16th October 2010, 12:38 PM
  3. Replies: 6
    Last Post: 15th September 2010, 10:56 PM
  4. How to read configuration file line by line in C ?
    By demuytree in forum Coding in General
    Replies: 1
    Last Post: 26th June 2009, 09:00 PM
  5. Replies: 1
    Last Post: 11th February 2008, 05:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •