0

I am reading a file (test.log.csv) line by line until the end of the file, and I want to extract the value at 4th column of current line read then output the value to a text file. (output.txt)

For example, right now I read until 2nd line (INSERT,SLT_TEST_1,TEST,1127192896,0,DEBUG1) and I want to extract the number at column 4 in the current line and output to a text file named as output.txt.

test.log.csv

INSERT,SLT_TEST_1,TEST,1127192896,0,DEBUG1
INSERT,SLT_TEST_1,TEST,1127192896,0,DEBUG1
INSERT,SLT_TEST_1,TEST,1127192896,0,DEBUG1

The desired output is

output.txt

1127192896
1127192896
1127192896

Right now my script is as below

#! /bin/bash
clear
rm /home/mobaxterm/Script/output.txt

while IFS= read -r line
do 
if [[ $line == *"INSERT"* ]] && [[ $line == *"$1"* ]]
then
    echo $line >> /home/mobaxterm/Script/output.txt
    lastID=$(awk -F "," '{if (NR==curLine) { print $4 }}' curLine="${lineCount}")
    echo $lastID
else
    if [ lastID == "$1" ]
    then
        echo $line >> /home/mobaxterm/Script/output.txt
    fi
fi
lineCount=$(($lineCount+1))
done < "/home/mobaxterm/Script/test.log.csv"

The parameter ($1) will be 1127192896

I tried declaring a counter in the loop and compare NR with the counter, but the script just stopped after it found the first one.

Xeon
  • 246
  • 3
  • 15
  • `awk -F, 'NR==2{print $2}' data.txt` maybe? – Mark Setchell Oct 13 '15 at 12:24
  • `cut -d, -f2 <<< $line` maybe? – Mark Setchell Oct 13 '15 at 12:26
  • Well, I have to read all the lines and extract the number at second column in the current line. Hardcoding it won't help. – Xeon Oct 13 '15 at 12:45
  • You haven't explained or given any hugh-level overview of what you are actually trying to achieve. – Mark Setchell Oct 13 '15 at 12:48
  • Yes, I am sorry about that. I have edited the question. Thanks for pointing out. Cheers. – Xeon Oct 13 '15 at 12:58
  • It is still unclear what you are trying to do! You have shown a script that doesn't work and which looks for the word "INSERT" in a file which clearly doesn't contain that word. What, in very simple terms, will be the output of your script? Is it 87 lines? Or one line? The second field on the line containing "INSERT"? The second field on every line that comes three lines after any line that contains "INSERT"? The second field on the line whose number is given by $1? – Mark Setchell Oct 13 '15 at 13:01
  • Sorry, modified the question. As to the number of lines, it's around 35k. – Xeon Oct 13 '15 at 13:10
  • Please create a reproducible example of what you are trying to do. http://stackoverflow.com/help/mcve. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – pcantalupo Oct 13 '15 at 13:13
  • Well the thing is, this is what I have right now. And the number of lines do not matter, since they are all the same as the sample I have posted above. – Xeon Oct 13 '15 at 13:19

2 Answers2

1

Find all the lines where the 4th field is 1127192896 and output the 4th field:

awk -F, -v SEARCH="1127192896" '$4 ~ SEARCH {print $4}' test.log.csv

1127192896
1127192896
1127192896

Find all the lines containing the word "INSERT" and where the 4th field is 1127192896

awk -F, -v SEARCH="1127192896" '$4 ~ SEARCH && /INSERT/ {print $4}' test.log.csv

If you have the number you want to look for in a variable called $1, put that in place of the 1127192896, like this:

awk -F, -v SEARCH="$1" '$4 ~ SEARCH && /INSERT/ {print $4}' test.log.csv
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

You can combine variable substitution and definition of array.

array_variable=( ${line//,/ /} )
sth_you_need=${array_variable[1]}

Or you can just use awk/cut

sth_you_need=$(echo $line | awk -F, 'NR==2{print $2}')
# or
sth_you_need=$(echo $line | cut -d, -f2)
yegong
  • 739
  • 1
  • 7
  • 15