Read line containing String (Bash)

10,234

Solution 1

Let me present you awk:

$ awk '/Device/ {print $2}' file
name1
name2
name3

This prints the second field on the lines containing Device. If you want to check that they start with Device, you can use ^Device:.

Update

To get the output you mention in your edited question, use this:

$ awk -v var="MainComputer" '/Device/ {print var, "->", $2}' a
MainComputer -> name1
MainComputer -> name2
MainComputer -> name3

It provides the variable name through -v and then prints the line.


Find some comments regarding your script:

file="/scripts/file.txt"
while read -r line
do
     if [$variable="Device"]; then # where does $variable come from? also, if condition needs tuning
     device='echo "$line"' #to run a command you need `var=$(command)`
echo $device #this should be enough
fi
done <file.txt #why file.txt if you already stored it in $file?

Check bash string equality to see how [[ "$variable" = "Device" ]] should be the syntax (or similar).

Also, you could say while read -r name value, so that $value would contain from the 2nd value on.

Solution 2

Alternatively, let me present you grep and cut:

$ grep "^Device:" $file | cut "-d " -f2-
Share:
10,234
Omnomnom
Author by

Omnomnom

To infinity and beyond

Updated on June 14, 2022

Comments

  • Omnomnom
    Omnomnom almost 2 years

    I have a file, and its something like this:

    Device: name1
    random text
    Device: name2
    random text
    Device: name3
    random text
    

    I have a variable: MainComputer

    What I want to get (for each name, i have like 40 names):

       MainComputer -> name1
       MainComputer -> name2
       MainComputer -> name3
    

    What I have:

    var="MainComputer"   
    var1=$(awk '/Device/ {print $3}' file)
    echo "$var -> $var1"
    

    This only gives the arrow "->" and the link for the first variable, I want them for the other 40 variables...

    Thanks anyway!

  • fedorqui
    fedorqui about 9 years
    I guess cut "-d " -f2- is a typo. You may want to say cut -d" "
  • Omnomnom
    Omnomnom about 9 years
    Excuse me, forgot this: variable="Device" file="/scripts/file.txt" while read -r line do if [$variable="Device"]; then device='echo "$line"' echo $device fi done <file.txt
  • anishsane
    anishsane about 9 years
    @fedorqui: How are they different? Both will be result to same argument value to cut. -d" " is just more readable to me. But from bash perspective, both are same. (That may not be the case for cygwin's cut.exe, if being run from cmd. cmd passes " to child process.)
  • Daniel Stenberg
    Daniel Stenberg about 9 years
    Exactly, you can just pick the version you like most.
  • Daniel Stenberg
    Daniel Stenberg about 9 years
    You need to replace $file with the real path, or assign the file variable first.
  • Omnomnom
    Omnomnom about 9 years
    fedorqui, that worked, ty! But now when i have the output, how do i put them in a variable?
  • fedorqui
    fedorqui about 9 years
    @anishsane Weird, I hadn't seen "-d " before. It is a bit strange since it sets the parameter and its value all together. But it's also interesting, good to know!
  • fedorqui
    fedorqui about 9 years
    @Omnomnom just say var=$(command). Or, in this case, var=$(awk '/Device/ {print $2}' file).
  • fedorqui
    fedorqui about 9 years
    @Omnomnom you just updated your question but what you say there is quite different from what you comment here. Try to clarify, please
  • Omnomnom
    Omnomnom about 9 years
    Yes, thats what i had before with Cut, now there is no enter between the names, and they are shown on 1 line...
  • fedorqui
    fedorqui about 9 years
    @Omnomnom Because to print them you need to quote the variable: echo "$var" will keep the format.
  • mklement0
    mklement0 about 9 years
    @fedorqui: Yes, "-d " is unusual, but, as @anishsane points out, -d" " results in the exact same string by the time cut sees it: both forms result in a single argument containing d followed by a space, after the shell has performed quote removal.
  • Omnomnom
    Omnomnom about 9 years
    Yes I will, still got 1 problem though... If that is solved I can continue scripting! And I know I'm new, but been using stackoverflow for a few years, just never posted something... I've edited the question...
  • fedorqui
    fedorqui about 9 years
    @mklement0 fantastic, wasn't aware of it. To make it "persistent", I just posted it as an answer in use space as a delimiter with cut command.
  • mklement0
    mklement0 about 9 years
    Nicely done; if using GNU grep is an option, a grep command alone will do: grep -Po '^Device: \K.*' "$file". Quibble: please double-quote $file.
  • Omnomnom
    Omnomnom about 9 years
    I'm gonna try this tomorrow but i'll think it will work, thanks!