Find the answer to your Linux question:
Results 1 to 4 of 4
Hello, I've file, with following content Code: sys true hide Basesystem desktop true show Xserver, Gnome, Kde, ... office true show Openoffice, TeX, Acroread, Groupwise, ... science false show gnuplot, ...
  1. #1
    Just Joined!
    Join Date
    Apr 2010
    Posts
    2

    subsitute parts from a file (information in a string)

    Hello,

    I've file, with following content

    Code:
    sys      true    hide    Basesystem
    desktop      true    show    Xserver, Gnome, Kde, ...
    office       true    show    Openoffice, TeX, Acroread, Groupwise, ...
    science      false   show    gnuplot, xmgrace, octave, R, ...
    Then I've string in a file
    "desktop" "science"

    With the information of that file I want change the previous file, in each line where the key word is in the string I want "true" in the second column, otherwise there should bei false...

    in this example


    Code:
    sys  false   hide    Basesystem
    desktop      true    show    Xserver, Gnome
    office       false    show    openoffice, TeX, 
    science      true   show    xmgrace, octave, R, ...
    (the seperator is tab ...)

    How can I do this ( in a nice way)?

    Ansir

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try:
    Code:
    awk 'NR==FNR{p=$1;s=$2;next}$1==p{$2=s}1' OFS="\t" file_with_string mainfile

  3. #3
    Just Joined!
    Join Date
    Apr 2010
    Posts
    2
    thank your for the answer, but i think it doesn't work for me (or my question was not clear enough)

    mainfile
    Code:
    sys	true	hide	Basesystem
    desktop	false	show	Xserver, Gnome, Kde, ...
    office	true	show	Openoffice, Groupwise, ...
    science	false	show	gnuplot, xmgrace, R, ..
    then I have a file dat.tmp

    Code:
    "desktop" "office"
    nothing happens

    when I remove the '"' an try

    awk 'NR==FNR{p=$1;s=$2;next}$1==p{$2=s}1' dat.tmp mainfile

    Code:
    sys     true    hide    Basesystem
    desktop office show Xserver, Gnome, Kde, ...
    office  true    show    Openoffice, Groupwise, ...
    science false   show    gnuplot, xmgrace, R, ..
    In the line of desktop there appears "office"....

    but I want the following

    mainfile
    Code:
    sys	false	hide	Basesystem
    desktop	true	show	Xserver, Gnome, Kde, ...
    office	true	show	Openoffice, Groupwise, ...
    science	false	show	gnuplot, xmgrace, R, ..
    the second column should be "true" when one can find a element of the string in it, otherwise the second column should bei false

    Ansir

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    I'm sorry, misread something, try this one:
    Code:
    awk '
    NR==FNR{
      gsub("\"","")
      p=$1;s=$2
      next
    }
    $1==p || $1==s{$2="true"}
    1' OFS="\t" dat.temp mainfile

Posting Permissions

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