1

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 
  • You don't need a convoluted loop over running through the file and use awk on each line. Using awk 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 for loop with the echo $i has undergone word-splitting and likely the string 1#04-06-2019 03:06:17 is stored in variable i as two separate strings.

    – Inian Jun 04 '19 at 20:00
  • Also Never use uppercase variable names in your script, they are reserved only for the environment variables used by the shell. Suggest taking a good course on shell scripting. Following https://mywiki.wooledge.org/BashGuide is one such useful resource. – Inian Jun 04 '19 at 20:00
  • I have just modified variable names for example and the actuals are different. I need a loop for two lines from the file. Can you please help in storing the complete timestamp 04-06-2019 03:06:17 in second_value variable – sasikanth konala Jun 04 '19 at 20:35
  • cat /var/tmp/file.txt

    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
  • @Inian uppercase variable naming is a convention not a requirement. Some of them are reserved variables, but not many. And it's perfectly possible to put a lowercase or mixedcase variable name into the environment; it's just convention that says we should or should not do this. – Chris Davies Jun 04 '19 at 21:18
  • Thanks a lot for all your help. I tried the syntax for first line from the file it worked. Can you please give me the syntax for four lines from the file content. If possible also tell me with loop. – sasikanth konala Jun 04 '19 at 22:03

3 Answers3

1

This should work.

second_val=$(cat /var/tmp/file.txt | awk -F '#' '{print $2}')

As rightly suggested in the comment, you can also do

second_val=$(awk -F '#' '{print $2}' /var/tmp/file.txt)
HarshaD
  • 366
0

You could simplify this by using a different format for the date output; convert your date field's data to the number of seconds since the Unix epoch (00:00:00, Jan 1, 1970) with

$ date -d "2019-06-04 14:02:32" +"%s"

and then insert that integer value into the date field.

K7AAY
  • 3,816
  • 4
  • 23
  • 39
0

Assuming you do want and need to loop in the shell over each line of the file (so assuming you're not just doing text processing), you could write it:

while IFS='#' read <&3 -r first_value second_value; do
  something with "$first_value" and "$second_value" 3<&-
done 3< "/var/tmp/${file}.txt"
  • as per Inian steps, It worked for one line from the file. Second line it is not working. So , I need the loop for atleast two lines. – sasikanth konala Jun 05 '19 at 14:27
  • as per Inian steps, It worked for one line from the file. Second line it is not working. So , I need the loop for atleast two lines.

    @Stephane , When trying with loop , values are not picking to fisrt_value and second_value

    1#05-06-2019 10:06:09

    • IFS='#'
    • read -r first_value second_value
    • 1 and '05-06-2019 10:06:09 '
    • IFS='#'
    • read -r first_value second_value
    • echo 'update stb_tab set second_value=to_date('''''','''dd-mm-yy h24:mi:ss''')

    where first_value=;'

    – sasikanth konala Jun 05 '19 at 15:00