Discussion:
loop with awk
MOKRANI Rachid rachid.mokrani@ifpen.fr [sed-users]
2017-06-01 16:08:05 UTC
Permalink
Hi,

I think it's very simple, but I don't find how to do it.

cat A.txt :

Mme;Sylvie;Blue;Paris;FR
Mr;Dan;Green;New-York;USA
Mr;David;Allen;Cannes;France

#!/bin/bash
for f in $(cat A.txt | awk -F ";" '{print $2"+"$3}') ;
do
echo "$f" | tr '[:upper:]' '[:lower:]'
done

OK, the result is :
sylvie+blue
dan+green
david+allen


But I want to add the result at the end of the each line of the file A.txt

Need result in a new file B.txt :

Mme;Sylvie;Blue;Paris;FR;sylvie+blue
Mr;Dan;Green;New-York;USA;dan+green
Mr;David;Allen;Cannes;France;david+allen

Regards.



__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________


[Non-text portions of this message have been removed]
Jim Hill gjthill@gmail.com [sed-users]
2017-06-01 16:28:50 UTC
Permalink
sed -E 's/([^;]*);([^;]*);([^;]*);.*/&;\L\2+\3/' fileA.txt

awk '{print $0,tolower($2"+"$3)}' FS=\; OFS=\; fileA.txt


[Non-text portions of this message have been removed]
MOKRANI Rachid rachid.mokrani@ifpen.fr [sed-users]
2017-06-02 07:44:36 UTC
Permalink
Thanks,

But I need to add the awk cmd in the loop.

In my exemple, I need to do a loop in a file and add the result of the loop for each line at the end of each line of the file A.txt. (I hope I’m clear
)

Regards.



De : Jim Hill [mailto:***@gmail.com]
Envoyé : jeudi 1 juin 2017 18:30
À : MOKRANI Rachid
Cc : sed-users
Objet : Re: loop with awk

sed -E 's/([^;]*);([^;]*);([^;]*);.*/&;\L\2+\3/' fileA.txt

awk '{print $0,tolower($2"+"$3)}' FS=\; OFS=\; fileA.txt





De : MOKRANI Rachid
Envoyé : jeudi 1 juin 2017 18:08
À : 'sed-***@yahoogroups.com'
Objet : loop with awk

Hi,

I think it’s very simple, but I don’t find how to do it.

cat A.txt :

Mme;Sylvie;Blue;Paris;FR
Mr;Dan;Green;New-York;USA
Mr;David;Allen;Cannes;France

#!/bin/bash
for f in $(cat A.txt | awk -F ";" '{print $2"+"$3}') ;
do
echo "$f" | tr '[:upper:]' '[:lower:]'
done

OK, the result is :
sylvie+blue
dan+green
david+allen


But I want to add the result at the end of the each line of the file A.txt

Need result in a new file B.txt :

Mme;Sylvie;Blue;Paris;FR;sylvie+blue
Mr;Dan;Green;New-York;USA;dan+green
Mr;David;Allen;Cannes;France;david+allen

Regards.




__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________


[Non-text portions of this message have been removed]
Davide Brini dave_br@gmx.com [sed-users]
2017-06-02 10:44:43 UTC
Permalink
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
Thanks,
But I need to add the awk cmd in the loop.
No you don't, unless there's some other requirement you didn't tell. Did
you try Jim's code? It produces the output you asked for in your first
email.
--
D.
MOKRANI Rachid rachid.mokrani@ifpen.fr [sed-users]
2017-06-02 11:07:42 UTC
Permalink
Hi,

Jim's code work perfectly. Thanks.

And yes I need to do other requirement (that I don't tell).


-----Message d'origine-----
De : sed-***@yahoogroups.com [mailto:sed-***@yahoogroups.com]
Envoyé : vendredi 2 juin 2017 12:46
À : sed-***@yahoogroups.com
Objet : Re: loop with awk
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
Thanks,
But I need to add the awk cmd in the loop.
No you don't, unless there's some other requirement you didn't tell. Did you try Jim's code? It produces the output you asked for in your first email.


--
D.


------------------------------------
Posted by: Davide Brini <***@gmx.com>
------------------------------------
--
------------------------------------

Yahoo Groups Links



__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses pièces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
Rachid Mokrani rachid.mokrani@gmail.com [sed-users]
2017-06-02 19:55:56 UTC
Permalink
Hi,

After some search, this is what I need.


cat A.txt
Mme;Sylvie;Blue;Paris;FR
Mr;Dan;Green;New-York;USA
Mr;David;Allen;Cannes;France


#!/bin/bash
while read -r line
do
echo "$line" |awk -F ";" '{print $2"+"$3}' | tr '[:upper:]' '[:lower:]'|awk
-v i="$line" '{print i";"$1}'
done < A.txt

result :

Mme;Sylvie;Blue;Paris;FR;sylvie+blue
Mr;Dan;Green;New-York;USA;dan+green
Mr;David;Allen;Cannes;France;david+allen


Regards.
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
Hi,
Jim's code work perfectly. Thanks.
And yes I need to do other requirement (that I don't tell).
-----Message d'origine-----
Envoyé : vendredi 2 juin 2017 12:46
Objet : Re: loop with awk
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
Thanks,
But I need to add the awk cmd in the loop.
No you don't, unless there's some other requirement you didn't tell. Did
you try Jim's code? It produces the output you asked for in your first
email.
--
D.
------------------------------------
------------------------------------
--
------------------------------------
Yahoo Groups Links
__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the
environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à
l'intention exclusive de ses destinataires. Toute utilisation non conforme
à sa destination, toute diffusion ou toute publication, totale ou
partielle, est interdite, sauf autorisation expresse. IFP Energies
nouvelles décline toute responsabilité au titre de ce message. This message
and any attachments are confidential and intended solely for the
addressees. Any unauthorised use or dissemination is prohibited. IFP
Energies nouvelles should not be liable for this message.
__________________________
[Non-text portions of this message have been removed]
Vlad Gershkovich vgersh99@gmail.com [sed-users]
2017-06-02 20:10:42 UTC
Permalink
you can do it all in awk:
awk -F';' '{$(NF+1)=tolower($2 "+" $3)}1' OFS=';' A.txt
Post by Rachid Mokrani ***@gmail.com [sed-users]
Hi,
After some search, this is what I need.
cat A.txt
Mme;Sylvie;Blue;Paris;FR
Mr;Dan;Green;New-York;USA
Mr;David;Allen;Cannes;France
#!/bin/bash
while read -r line
do
echo "$line" |awk -F ";" '{print $2"+"$3}' | tr '[:upper:]' '[:lower:]'|awk
-v i="$line" '{print i";"$1}'
done < A.txt
Mme;Sylvie;Blue;Paris;FR;sylvie+blue
Mr;Dan;Green;New-York;USA;dan+green
Mr;David;Allen;Cannes;France;david+allen
Regards.
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
Hi,
Jim's code work perfectly. Thanks.
And yes I need to do other requirement (that I don't tell).
-----Message d'origine-----
Envoyé : vendredi 2 juin 2017 12:46
Objet : Re: loop with awk
On Fri, 2 Jun 2017 07:44:36 +0000, "MOKRANI Rachid
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
Thanks,
But I need to add the awk cmd in the loop.
No you don't, unless there's some other requirement you didn't tell. Did
you try Jim's code? It produces the output you asked for in your first
email.
--
D.
------------------------------------
------------------------------------
--
------------------------------------
Yahoo Groups Links
__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the
environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à
l'intention exclusive de ses destinataires. Toute utilisation non
conforme
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
à sa destination, toute diffusion ou toute publication, totale ou
partielle, est interdite, sauf autorisation expresse. IFP Energies
nouvelles décline toute responsabilité au titre de ce message. This
message
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
and any attachments are confidential and intended solely for the
addressees. Any unauthorised use or dissemination is prohibited. IFP
Energies nouvelles should not be liable for this message.
__________________________
[Non-text portions of this message have been removed]
--
*Vlad Gershkovich*
#include <disclaimer.h>


[Non-text portions of this message have been removed]
Cameron Simpson cs@zip.com.au [sed-users]
2017-06-02 21:52:43 UTC
Permalink
Post by Rachid Mokrani ***@gmail.com [sed-users]
After some search, this is what I need.
cat A.txt
Mme;Sylvie;Blue;Paris;FR
Mr;Dan;Green;New-York;USA
Mr;David;Allen;Cannes;France
I'm glad you've found a valid solution, and doubly glad that you'd reported it
back to the list. But it is generally good to provide some of the wider context
to your question - in your case the reason you are obliged to use a shell loop
in the solution.
Post by Rachid Mokrani ***@gmail.com [sed-users]
#!/bin/bash
As is usually the case with all shell script, you do not need to specify bash;
nothing in your scipt (or in most scripts) makes use of features specific to
bash. All POSIX systems (== almost all UNIX and Linux systems (and Macs are
also UNIX)) have /bin/sh, and that is what you should specify for shell
scripts:

#!/bin/sh

It will always be there, and it will always have that location. By contrast,
not all systems have bash and of those which do, not all keep it in /bin.
Post by Rachid Mokrani ***@gmail.com [sed-users]
while read -r line
Just bear in mind that 'read -r' does strip some leading a trailing whitespace;
for your needs it is fine, and in most circumstances it is fine, an in the
shell it if pretty much the best you can do anyway if the shell itself must
work on each line.
Post by Rachid Mokrani ***@gmail.com [sed-users]
do
echo "$line"
Some echoes interpret escapes in the text, such as '\c'. A more robust thing to
say is this:

printf '%s\n' "$line"


|awk -F ";" '{print $2"+"$3}' | tr '[:upper:]' '[:lower:]'|awk -v i="$line" '{print i";"$1}'

I am curious as to why you didn't run with the earlier solution from Jim which
can do this in one line, even if you're using the shell to process each line on
its own. Since awk can convert things to lower case, why not use that instead
of stepping through "tr"? Untested example based on Jim's code:

printf '%s\n' "$line" \
| awk '-F;' -v 'OFS=;' '{ print $0, tolower($2 "+" $3) }'

This uses several fewer processes, resulting in faster code.

Finally, it is usually good to fold long pipelines as I have above, so that
each step has its own line. It makes things much easier to read and change.

This folding generally comes in two styles:

command1 | \
command2

or:

command1 \
| command2

You can see that I prefer the latter; it makes the continued pipeline very
obvious at the start of the next line; semanticly they are they same, I just
consider the second more readable.

Cheers,
Cameron Simpson <***@zip.com.au>
dgoldman@ehdp.com [sed-users]
2017-06-03 01:26:57 UTC
Permalink
"I've got a few remarks about your solution:" - That is very helpful to the OP. FWIW, I prefer "folding style" #1. :) Just personal preference. Actually, I would instead use temporary files, skip long pipelines. And speed is usually a very secondary consideration, IMO. I think readability and maintainability are usually much more important. "Avoid premature optimization". Anyway, it was nice of you to help educate others.




[Non-text portions of this message have been removed]
Cameron Simpson cs@zip.com.au [sed-users]
2017-06-03 01:59:40 UTC
Permalink
Post by ***@ehdp.com [sed-users]
"I've got a few remarks about your solution:" - That is very helpful to the
OP. FWIW, I prefer "folding style" #1. :) Just personal preference. Actually,
I would instead use temporary files, skip long pipelines. And speed is usually
a very secondary consideration, IMO. I think readability and maintainability
are usually much more important. "Avoid premature optimization". Anyway, it
was nice of you to help educate others.
Just regarding speed, with shell scripts the runtime is often dominated by the
fork/exec cost because so many operations are farmed out to subprocesses.
Because of this it is usually worth spending some thinking time on reducing the
number of commands dispatched for the work (without making the script
unreadable or unmaintainable). I agree about premature optimisation, but the
threshold for _some_ fork/exec reduction is lower with the shell than in other
languages.

Cheers,
Cameron Simpson <***@zip.com.au>
dgoldman@ehdp.com [sed-users]
2017-06-03 22:11:50 UTC
Permalink
"usually worth spending some thinking time on reducing the
number of commands dispatched for the work (without making the script
unreadable or unmaintainable). " - I think that is very good advice for everyone.

[Non-text portions of this message have been removed]

Loading...