1

This is my script. It print every row in the file with the number of row. Next i want to read which row user choosed and save it to some variable.

  I=1
    for ROW in $(cat file.txt)
    do
        echo "$I $ROW"
        I=`expr $I + 1`
    done
    read var
    awk 'FNR = $var {print}' file.txt

Then i want to to print / save the chosen row into the file.

How can I do this ?

when i echo $var it shows me properly the number. But when i'm trying to use this variable in awk, it print every line.

How to read the 'var' line from file?

And moreover, how to save this line in other variable?

Example file.txt

1 line1
2 line2
3 line3
4 line4

when i tap 3 i want to read third line from file.

  • 1
    Never do this: `for x in $(command)` or \`command\` or $var. for-in is used for iterating arguments, not (output) strings. Instead, use a glob (eg. *.txt), arrays (eg. "${names[@]}") or a while-read loop (eg. while read -r line). See http://mywiki.wooledge.org/BashPitfalls#pf1 and http://mywiki.wooledge.org/DontReadLinesWithFor – Rany Albeg Wein Jan 26 '16 at 21:44

4 Answers4

1

Try this:

cat -n file.txt; read var; line="$(sed -n ${var}p file)"; echo "$line"

With more focus on Dryingsoussage's version:

#!/bin/bash

file="file.txt"
declare -i counter=0   # set integer attribute
var=0

while read -r line; do
  counter=counter+1
  printf "%d %s\n" "$counter" "$line"
done < "$file"

# check for number and greater-than 0 and less-than-or-equal $counter
until [[ $var =~ ^[0-9]+$ ]] && [[ $var -gt 0 ]] && [[ $var -le $counter ]]; do
  read -p "Enter line number:" var
done

awk -v var="$var" 'FNR==var {print}' "$file"
Cyrus
  • 84,225
  • 14
  • 89
  • 153
0

You cannot use $varname inside ' ' they will not be resolved.

look at this other post it should help you: How to use shell variables in an awk script

Community
  • 1
  • 1
Rob
  • 2,618
  • 2
  • 22
  • 29
0

First: You cannot use $var in a single quotes, as echo '$var' would be plain $var, no its value.
Second: You used = (assignment) operator instead of == (equality) operator.
Third: You don't have to write { print } if you want the line to be printed. You can write nothing instead.
Fourth: As was explained in the deleted comment below - do not allow bash expanding the variables in the awk script code, as it can lead to code injection. So conclusion is:

awk -v var="$var" 'FNR == var' file.txt

should do what you want.

nsilent22
  • 2,763
  • 10
  • 14
0
cat -n file.txt
read var
row="$(awk -v tgt="$var" 'NR==tgt{print;exit}' file.txt)"
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • This has shown up in the "Low Quality Posts" review queue, even though it's not - maybe triggered by code-only? – Benjamin W. Jan 27 '16 at 05:20
  • I'll approve it, but perhaps the author might want to add some explanation. – rghome Jan 27 '16 at 09:22
  • 1
    There's nothing to explain, it just is what it says it is.... Not all answers require additional text and IMHO the simpler ones are better without it so the reader who doesn't understand it has to go look up a couple of simple constructs and learns more from that than being spoon-fed. – Ed Morton Jan 27 '16 at 11:10