0

I am learning bash scripting and trying to use awk to find a word or two from one file, and then replace a placeholder in a template text file with the word saved in awk. Looks like this:

echo "What's the class name?"
read $CLASS

and then use the class as a pattern to find a particular line and column:

NAME=$(awk '/$CLASS/ {print $2}' info.txt

and replace the word in a template with SED:

sed -i 'a/Name/$NAME/g' $FILE

I need to do this for several different columns and different parts of the template, so I can't save the whole awk command in a variabl. right now as it is, it is taking the whole column instead of just the line beginning with the pattern. (So i get a bunch of names instead of just one)

I have tried it a million ways and can't seem to get it to work properly. Any insight? This is my first post to Stack Overflow! :)

EDIT: the file I'm getting information FROM would look something like this:

stac1000 Joe 700
dbas1400 Sally 701
prog 1200 Rick 702

the template file that I'm replacing values of is a cover page template. I copy it to a new file and then replace the following values with ones from the info file. The aim is to be able to make several files that look the same but with different information from different lines in the info text:

Class
Name
Section
Angela Kay
  • 45
  • 9
  • bash variables don't get expanded in single quotes. – Etan Reisner Apr 15 '15 at 14:22
  • 1
    @Etan it solves half the problem but it looks like there are a couple more – Tom Fenech Apr 15 '15 at 14:23
  • I've tried double quotes, single quotes, putting the pattern in double quotes and the print part in single quotes and vice versa, escaping the slashes, putting the file after 'awk', putting a < and no dice. I can make it work directly in the CLI just not in the script... – Angela Kay Apr 15 '15 at 14:25
  • 1
    To get a specific column and row in `awk` you say `awk 'NR==5 {print $2}' file` (column 2, row 5), for example. And if you want to print the 2nd field in the row containing the bash variable `$CLASS`, you say `awk -v class="$CLASS" '$0 ~ class {print $2}'`, etc. However, this is just guessing. Can you post a sample of a template text file so we can guess more specifically what you are trying to do? – fedorqui Apr 15 '15 at 14:28
  • @TomFenech I'm not sure. Certainly doing this efficiently requires more than just that detail but I think that detail is enough to make this current approach work (inefficiently). But I don't mind retracting my vote. – Etan Reisner Apr 15 '15 at 14:30
  • 1
    @AngelaKay, note that `ECHO`, `READ` and `AWK` are unlikely to be valid commands. Case matters. – glenn jackman Apr 15 '15 at 14:33
  • @glennjackman yes, you're right. I have them in lowercase in my file. Not sure why I capitalized them here, my brain is going to mush. – Angela Kay Apr 15 '15 at 14:35
  • We are getting there. The sample file looks fine (well, a header with the names of the columns would help), now we would like to know what exactly the output should look like. In general, you may want to check [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) to get proficient at this :) – fedorqui Apr 15 '15 at 14:41
  • @fedorqui yep I am having a hard time explaining the problem, haha! I had a look at the other question that someone else posted about variables and I think that sort of helps, however now it isn't saving anything in the $NAME variable. Esentially I want to get one name from one of the lines of the file, using the class (given by user) as a pattern to find the right line. – Angela Kay Apr 15 '15 at 14:50
  • @AngelaKay did you try my previous suggestion `awk -v class="$CLASS" '$0 ~ class {print $2}' file`? It should be the way to do it. – fedorqui Apr 15 '15 at 15:00
  • 1
    @fedorqui I just did, I think it worked!! Thank you, you're the best! – Angela Kay Apr 15 '15 at 15:06

0 Answers0