4

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!

Omnomnom
  • 111
  • 2
  • 2
  • 11

2 Answers2

11

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.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 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 – Omnomnom Apr 22 '15 at 12:15
  • fedorqui, that worked, ty! But now when i have the output, how do i put them in a variable? – Omnomnom Apr 22 '15 at 12:28
  • @Omnomnom just say `var=$(command)`. Or, in this case, `var=$(awk '/Device/ {print $2}' file)`. – fedorqui Apr 22 '15 at 12:38
  • @Omnomnom you just updated your question but what you say there is quite different from what you comment here. Try to clarify, please – fedorqui Apr 22 '15 at 12:40
  • Yes, thats what i had before with Cut, now there is no enter between the names, and they are shown on 1 line... – Omnomnom Apr 22 '15 at 12:41
  • @Omnomnom Because to print them you need to quote the variable: `echo "$var"` will keep the format. – fedorqui Apr 22 '15 at 12:42
  • 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... – Omnomnom Apr 22 '15 at 12:59
  • 1
    I'm gonna try this tomorrow but i'll think it will work, thanks! – Omnomnom Apr 22 '15 at 14:58
5

Alternatively, let me present you grep and cut:

$ grep "^Device:" $file | cut "-d " -f2-
Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222
  • I guess `cut "-d " -f2-` is a typo. You may want to say `cut -d" "` – fedorqui Apr 22 '15 at 12:13
  • 2
    @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.) – anishsane Apr 22 '15 at 12:20
  • Exactly, you can just pick the version you like most. – Daniel Stenberg Apr 22 '15 at 12:21
  • You need to replace $file with the real path, or assign the file variable first. – Daniel Stenberg Apr 22 '15 at 12:28
  • @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 Apr 22 '15 at 12:38
  • 1
    @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_. – mklement0 Apr 22 '15 at 12:59
  • 1
    @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](http://stackoverflow.com/a/29798447/1983854). – fedorqui Apr 22 '15 at 13:04
  • 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`. – mklement0 Apr 22 '15 at 13:13