I have these lines in file :
postgres 2609 21030 0 12:49 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 2758 21030 0 12:51 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 28811 21030 0 09:26 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 32200 21030 0 11:40 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 32252 21030 0 11:41 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
I need to separate second column values to process them.I have done this code :
pid=$(cat idle_log.txt | cut -d" " -f2)
echo $pid
but it only gave me 28811 32200 32252 in results.as you see there is no trace of 2609 2758 in list,I want to get them too. Also I want count them after extracting pids. I used :
npid=$(grep -o " " <<< $pid | grep -c .)
it returns 2 for results of 28811 32200 32252 I need it return 3 as count of processes. finally I want to process some thing line by line like in a loop with while but output of commands return results at once,and I can't process them in loop format and one by one.
thank you all for help.
$ cat data
postgres 2609 21030 0 12:49 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 2758 21030 0 12:51 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 28811 21030 0 09:26 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 32200 21030 0 11:40 ? 00:00:00 postgres: postgres postgres [local] idle in transaction
postgres 32252 21030 0 11:41 ? 00:00:00 postgres: postgres postgres [local] idle in transaction I need to extract second column from each line,
$ awk '{print $2}' data
2609
2758
28811
32200
32252
or you can squeeze multiple spaces into 1 using tr
and then use cut
like this:
$ tr -s ' ' < data | cut -d ' ' -f 2
2609
2758
28811
32200
32252
Edit:
$ tr -s ' ' < data | cut -d ' ' -f 2 | while read -r line || [[ -n "$line" ]]; do
> echo "$line" #put your custom processing logic here
> done
2609
2758
28811
32200
32252