I want to print timestamp as one variable from the file content. I need awk command to ignore space between data and time. The value to be printed in $2 as SECOND_VALUE.
ex:
$ cat /var/tmp/file.txt
1#04-06-2019 03:06:17
Code:
list=`cat /var/tmp/${file}.txt`
for i in $list
do
FIRST_VALUE=`echo $i|awk -F'#' '{print $1}'`
SECOND_VALUE=`echo $i|awk -F '#' '{print $2}'`
done
output:
++ cat /var/tmp/file.txt
+ list='1#04-06-2019 03:06:17' '
+ for i in '$list'
++ echo 1#04-06-2019
++ awk -F# '{print $1}'
+ FIRST_VALUE=1
++ echo 1#04-06-2019
++ awk -F '#' '{print $2}'
+ SECOND_VALUE=04-06-2019
Exptected output:
SECOND_VALUE=04-06-2019 03:06:17
awk
on each line. Usingawk
alone is self-sufficient as it is a lint oriented tool itself.FIRST_VALUE=$(awk -F'#' '{print $1}' /var/tmp/file.txt); SECOND_VALUE=$(awk -F'#' '{print $2}' /var/tmp/file.txt)
is all you need. The reason for your problem being the
– Inian Jun 04 '19 at 20:00for
loop with theecho $i
has undergone word-splitting and likely the string1#04-06-2019 03:06:17
is stored in variablei
as two separate strings.1#04-06-2019 03:06:17
2#04-06-2019 04:06:17
The file has two lines. will the syntax work for two lines or for only line from the file
– sasikanth konala Jun 04 '19 at 20:40