process substitution with awk, output splitting incorrectly
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
process substitution with awk, output splitting incorrectly
I have the following code to extract two dates using awk, which are then read into two awk variables new and old respectively.
Each dates on the html file pulled with curl request is in this format:
2015-04-06 09:40:37
And two are being extracted
However the strings are being split on white space within the date strings. I tried changing OFS to ',', but it was still splitting incorrectly.
Both parts of the date are being assigned to each variable using $3 and $4, then the space needs to be added back in so that the string can be used afterwards with a date command.
I just can't work out what is wrong, any help would be very much appreciated! Thanks!
Hi,
the culprit here is the shell (bash?), rather than awk. awk outputs a line that contains spaces and read splits it. If you want to prevent that, you need to change the IFS variable in the shell:
Hi,
the culprit here is the shell (bash?), rather than awk. awk outputs a line that contains spaces and read splits it. If you want to prevent that, you need to change the IFS variable in the shell:
Thanks! I did actually have IFS="," followed by the read command on a new line it will work? So you are saying it needs to all be on the same line and it will work? I can't try it now until the server is up again tomorrow when we are testing. Will let you know how I go
Thanks! I did actually have IFS="," followed by the read command on a new line it will work? So you are saying it needs to all be on the same line and it will work? I can't try it now until the server is up again tomorrow when we are testing. Will let you know how I go
do you mean something like this?
Code:
IFS=","
read dateStrNew dateStrOld < ...
That would also work. The difference is in this case the IFS variable will be set to "," permanently for the current shell, while the former assignment will only change IFS for the single command. If that doesn't work for you, then there is some other problem and it would help to show us the exact output of
Ah, well it's good to know that is a way of temporarily setting the IFS.
Yes I did.
Sample output:
newest ,
oldest
newest 2015-04-07 06
oldest 29:29,2015-04-06 09:40:37
newest ,
oldest
newest 2015-04-07 06
oldest 29:29,2015-04-06 09:40:37
So you can see that the comma is being read in the first variable, followed by whitespace/nothing.
Then the first part of the first date string
Then the second part of the first date string plus all of the second date string
then a comma
etc
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.