how to read rows? - bash
I'm trying to read first row from the file
> source ./rank file
using this script
set line = ($<) <- inside rank
but when I enter
echo $line I receive nothing, how can I change it? thanks in advance
Since csh is brain-dead, you'll have to do something like this:
set line = `head -n 1 filename`
It's built-in in Bash as:
read -r line < filename
set line = `cat file | sed 1q`
Related
read file to variable shell script
I have a text file that has coordinates in it. The text file looks like this: 52.56747345 -1.30973574 What I would like to do within raspberry pi shell script is to read the file and then create two variables. One being latitude which is the first value in the text file and the second being longitude which is the second value. Im not sure how to do this so could i please get some help.
This works ok: $ { read lat;read lon; } <file First line is stored in var $lat, second line in var $lon
lat=$(head -1 file.txt) echo $lat 52.56747345 lon=$(tail -1 file.txt) echo $lon -1.30973574
1 You have a data file: cat data.txt result: 52.56747345 -1.30973574 42.56747345 -2.30973574 32.56747345 -3.30973574 2 Write a shell script: cat tool.sh result: #!/bin/bash awk '{if(NR%2==0) print $0;else printf $0" "}' data.txt | while read latitude longitude do echo "latitude:${latitude} longitude:${longitude}" done 3 Execute this shell script.Output is like this: sh tool.sh result: latitude:52.56747345 longitude:-1.30973574 latitude:42.56747345 longitude:-2.30973574 latitude:32.56747345 longitude:-3.30973574
Formatting need to change for text file in bash scripting
I have below output from a text file. This is long file i just copy here some rows only. HP83904B74E6 13569.06 7705.509999999999 HP4DC2EECAA8 4175.1 2604.13 And i want to print it like below. HP83904B74E6 13569.06 7705.509999999999 HP4DC2EECAA8 4175.1 2604.13 I have tried by reading the file line by live using while loop and try to store the value of variable e.g. variablename$i so that i can print it like variablename0 and after every 3 line i have used If statement to print the value of variablename0 variablename1 variablename2, but did not work for me.
Use pr: $ pr -a3t tmp.txt HP83904B74E6 13569.06 7705.509999999999 HP4DC2EECAA8 4175.1 2604.13
i have tried by reading the file line by live using while loop and try to store the value of variable e.g. variablename$i so that i can print it like variablename0 and after every 3 line i have used If statement to print the value of variablename0 variablename1 variablename2, but did not work for me. I am just learning bash. while read -r a; do read -r b; read -r c; echo "$a $b $c"; done < file you get, HP83904B74E6 13569.06 7705.509999999999 HP4DC2EECAA8 4175.1 2604.13
Bash Interpreter read
#!/bin/bash read k read m read fileName head -n -$k $fileName | tail -n +$m $fileName Hi, this is what I have now. I have to create bash interpreter that removes the lines from head and removes lines from tail in text file. I have to excute this bash interpreter by this way. ./strip.sh 2 3 hi.txt > bye.txt How do I read 2 for k and 3 for m? Also how do I read hi.txt for fileName? Also is my code correct? Please answer these question. I am really new to this bash interpreter. Thank you.
read is for reading values from standard input. What you want are positional parameters: k=$1 m=$2 fileName=$3 head -n -$k "$fileName" | tail -n +$m Drop the final argument of fileName from tail, because it will read from standard input (fed by the standard output of head), not a named file.
#chepner point you in the correct direction but the script as is at the moment does not work as you expected. If you want to try another approach: #!/bin/bash set -u k=${1} file=${3} lines=$(wc -l < "${file}") m=$[${lines}-${2}] awk 'NR>'${k}' && NR<='${m} "${file}" Where: set -u: make undefined variables (such as missing params) stop the execution $(wc -l < "${file}"): count the total lines of the file. $[${lines}-${2}]: subtract from bottom lines desired (passed to remove from the total number of lines awk 'NR>k && NR<=m': print (default awk behaviour) the lines > k and <= m
appending text to specific line in file bash
So I have a file that contains some lines of text separated by ','. I want to create a script that counts how much parts a line has and if the line contains 16 parts i want to add a new one. So far its working great. The only thing that is not working is appending the ',' at the end. See my example below: Original file: a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a b,b,b,b,b,b a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a b,b,b,b,b,b a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a Expected result: a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,xx b,b,b,b,b,b a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a b,b,b,b,b,b a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,xx This is my code: while read p; do if [[ $p == "HEA"* ]] then IFS=',' read -ra ADDR <<< "$p" echo ${#ADDR[#]} arrayCount=${#ADDR[#]} if [ "${arrayCount}" -eq 16 ]; then sed -i "/$p/ s/\$/,xx/g" $f fi fi done <$f Result: a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a ,xx b,b,b,b,b,b a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a b,b,b,b,b,b a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a ,xx What im doing wrong? I'm sure its something small but i cant find it..
It can be done using awk: awk -F, 'NF==16{$0 = $0 FS "xx"} 1' file a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,xx b,b,b,b,b,b a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a b,b,b,b,b,b a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,xx -F, sets input field separator as comma NF==16 is the condition that says execute block inside { and } if # of fields is 16 $0 = $0 FS "xx" appends xx at end of line 1 is the default awk action that means print the output
For using sed answer should be in the following: Use ${line_number} s/..../..../ format - to target a specific line, you need to find out the line number first. Use the special char & to denote the matched string The sed statement should look like the following: sed -i "${line_number}s/.*/&xx/" I would prefer to leave it to you to play around with it but if you would prefer i can give you a full working sample.
Append index variable at the end of each line
I am trying to append an index variable at the end of each line of a file that I have. However I dont want to lose the escape characters that I have in the textFile and thus cannot echo into the file again. Here's what I tried: while read p; do tempCom+=$p tempCom+=$indexVar echo $tempCom >> otherFile.txt tempCom="" done < result.txt What I am after: Read: "asdasdasdasdasdasd\ asdasd/asda" "qweqweqweqweqweqwe\ qweqwe/qweq" Output: "asdasdasdasdasdasd\ asdasd/asda" 1 "qweqweqweqweqweqwe\ qweqwe/qweq" 2 Note that indexVar is an index that is stored elsewhere and does not necessarily correspond to the line that its being appended to.
Your problem is very likely a quoting problem. Observe the IFS= and the -r option in the read statement too. while IFS= read -r p tempCom+=$p$indexVar printf '%s\n' "$tempCom" >> otherFile.txt # Observe the quotes tempCom= done < result.txt
If you just want to append the line number to the end why not use awk? awk '{print $0, "\t", NR}' < file.txt EDIT 1: It sounds like you want to use paste then (assuming you want to just join line by line) paste file1.txt file2.txt > fileresults.txt EDIT 2: You can use sed then: sed "s|$|${indexVar}|" input
Use the -r option of the read command, so that the backslashes are preserved. while read -r p; do