Originally posted by MoonRise
View Post
Announcement
Collapse
No announcement yet.
Problem using AWK in shell script
Collapse
This topic is closed.
X
X
-
Windows no longer obstructs my view.
Using Kubuntu Linux since March 23, 2007.
"It is a capital mistake to theorize before one has data." - Sherlock Holmes
- Top
- Bottom
-
- Top
- Bottom
Comment
-
Got it.
Clarification: When I open the extracted .txt file and expand the window so there isn't any line wrapping, I see extra white spaces between some columns on some of the lines. I'm assuming that the columns starting position should be the same for every line, but in the context of what you are doing with the line contents using awk, the extra spacing won't/doesn't matter, correct?
This is the extracted .txt file as I see it in a terminal using cat:
Pauls-iMacownloads paul$ cat EDI_867_20181118182952.TXTCode:ZZ9999997 00500011 XYZ DC SALT LAKE CITY 0050001131 20171014 0922 20171014 00999111101 20171014 0926586923 20171010 1 44.7 Lst East SUPP CH SRV EDI #999 9920 W. EntrtyPLEX DR KAYSVILLE UT 84037-959 EA 990003055 ZZ9999997 00500011 XYZ DC SALT LAKE CITY 0050001131 20171014 0922 XY9999997 20171014 00999111101 20171014 0926627797 20171010 4 178.8 SUPLYY DEPTSTRE #9 9900 WEST SOMEWHER AVENUE NAMPA ID 83687 EA 990003055 ZZ9999997 00500011 XYZ DC SALT LAKE CITY 0050001131 20171014 0922 XY9999997 20171014 00999111101 20171014 0926751452 20171012 1 44.7 SUPLYY DEPTSTRE #9 9900 WEST SOMEWHER AVENUE NAMPA ID 83687 EA 990003055 ZZ9999998 00500011 YZZ DC SACRAMENTO 0050001131 20171014 0922 YZ9999998 20171014 00999112501 20171014 0926516685 20171007 1 62.45 Lst East SUPPL CH SRV EDI #99 9920 West EntrtyPLEX DR. VISALIA CA 93291-623 EA 990003055 ZZ9999998 00500011 YZZ DC SACRAMENTO 0050001131 20171014 0922 YZ9999998 20171014 00999112501 20171014 0926678606 20171011 1 62.45 Lst East SUPPL CH SRV EDI #99 9920 West EntrtyPLEX DR. VISALIA CA 93291-623 EA 990003055 ZZ9999998 00500011 YZZ DC SACRAMENTO 0050001131 20171014 0922 YZ9999998 20171014 00999112501 20171014 0926515844 20171007 2 124.9 SUPLYY DEPTSTRE CHAN #9 9900 E SOMEWHR AVENUE FRESNO CA 93720 EA 990003055 ZZ9999999 00500011 ZZZ DC ORLANDO VISTA 0050001131 20171014 0922 20171014 00999111101 20171014 0926584818 20171010 1 44.7 SUPLYY DEPTSTRE CHAIN #9 9900 E SOMEWHR AVENUE, STE 9 ESTON FL 33331-350 EA 990003055 ZZ9999999 00500011 ZZZ DC ORLANDO VISTA 0050001131 20171014 0922 YZ9999998 20171014 00999113401 20171014 0926715863 20171012 3 256.5 SUPLYY DEPTSTRE CHAIN #9 LLC 900 SOMEWHR, STE 9 ESTON MIAMI FL 33126 EA 990003055 ZZ9999999 00500011 ZZZ DC ORLANDO VISTA 0050001131 20171014 0922 ZZ9999999 20171014 00999111101 20171014 0926548404 20171009 1 44.7 SUPPLY DEPTSTRE CHAINS LLC 900 SOMEWHER AVE MIAMI FL 33155 EA 990003055 ZZ9999999 00500011 ZZZ DC ORLANDO VISTA 0050001131 20171014 0922 XY9999997 20171014 00999113201 20171014 0926712287 20171012 2 110.1 Lst East SUPPL CH SRV EDI #9 9920 West EntrtyPLEX RD OCOEE FL 34761 EA 990003055
Windows no longer obstructs my view.
Using Kubuntu Linux since March 23, 2007.
"It is a capital mistake to theorize before one has data." - Sherlock Holmes
- Top
- Bottom
Comment
-
Originally posted by Snowhog View PostGot it.
Clarification: When I open the extracted .txt file and expand the window so there isn't any line wrapping, I see extra white spaces between some columns on some of the lines. I'm assuming that the columns starting position should be the same for every line, but in the context of what you are doing with the line contents using awk, the extra spacing won't/doesn't matter, correct?
This is the extracted .txt file as I see it in a terminal using cat:
Pauls-iMacownloads paul$ cat EDI_867_20181118182952.TXTCode:ZZ9999997 00500011 XYZ DC SALT LAKE CITY 0050001131 20171014 0922 20171014 00999111101 20171014 0926586923 20171010 1 44.7 Lst East SUPP CH SRV EDI #999 9920 W. EntrtyPLEX DR KAYSVILLE UT 84037-959 EA 990003055 ZZ9999997 00500011 XYZ DC SALT LAKE CITY 0050001131 20171014 0922 XY9999997 20171014 00999111101 20171014 0926627797 20171010 4 178.8 SUPLYY DEPTSTRE #9 9900 WEST SOMEWHER AVENUE NAMPA ID 83687 EA 990003055 ZZ9999997 00500011 XYZ DC SALT LAKE CITY 0050001131 20171014 0922 XY9999997 20171014 00999111101 20171014 0926751452 20171012 1 44.7 SUPLYY DEPTSTRE #9 9900 WEST SOMEWHER AVENUE NAMPA ID 83687 EA 990003055 ZZ9999998 00500011 YZZ DC SACRAMENTO 0050001131 20171014 0922 YZ9999998 20171014 00999112501 20171014 0926516685 20171007 1 62.45 Lst East SUPPL CH SRV EDI #99 9920 West EntrtyPLEX DR. VISALIA CA 93291-623 EA 990003055 ZZ9999998 00500011 YZZ DC SACRAMENTO 0050001131 20171014 0922 YZ9999998 20171014 00999112501 20171014 0926678606 20171011 1 62.45 Lst East SUPPL CH SRV EDI #99 9920 West EntrtyPLEX DR. VISALIA CA 93291-623 EA 990003055 ZZ9999998 00500011 YZZ DC SACRAMENTO 0050001131 20171014 0922 YZ9999998 20171014 00999112501 20171014 0926515844 20171007 2 124.9 SUPLYY DEPTSTRE CHAN #9 9900 E SOMEWHR AVENUE FRESNO CA 93720 EA 990003055 ZZ9999999 00500011 ZZZ DC ORLANDO VISTA 0050001131 20171014 0922 20171014 00999111101 20171014 0926584818 20171010 1 44.7 SUPLYY DEPTSTRE CHAIN #9 9900 E SOMEWHR AVENUE, STE 9 ESTON FL 33331-350 EA 990003055 ZZ9999999 00500011 ZZZ DC ORLANDO VISTA 0050001131 20171014 0922 YZ9999998 20171014 00999113401 20171014 0926715863 20171012 3 256.5 SUPLYY DEPTSTRE CHAIN #9 LLC 900 SOMEWHR, STE 9 ESTON MIAMI FL 33126 EA 990003055 ZZ9999999 00500011 ZZZ DC ORLANDO VISTA 0050001131 20171014 0922 ZZ9999999 20171014 00999111101 20171014 0926548404 20171009 1 44.7 SUPPLY DEPTSTRE CHAINS LLC 900 SOMEWHER AVE MIAMI FL 33155 EA 990003055 ZZ9999999 00500011 ZZZ DC ORLANDO VISTA 0050001131 20171014 0922 XY9999997 20171014 00999113201 20171014 0926712287 20171012 2 110.1 Lst East SUPPL CH SRV EDI #9 9920 West EntrtyPLEX RD OCOEE FL 34761 EA 990003055
You can run that AWK command from console, you will have to explicitly state the file name of course, and it will do just that. I was initially ecstatic about it until I tried to put in a bash script.
One of the problems is I am good at coding but I don't get to do it often so some things I have to go over again. You should see my Note folder of all things programming. Didn't help here though.Last edited by MoonRise; Nov 19, 2018, 01:09 PM.
- Top
- Bottom
Comment
-
Here is the output when run from konsole:
Code:ZZ9999997 00500011 XYZ DC SALT LAKE CITY 0050001131 20171014 0922 XY9999997 20171014 00999111101 20171014 0926627797 20171010 4 178.8 SUPLYY DEPTSTRE #9 9900 WEST SOMEWHER AVENUE NAMPA ID 83687 EA 990003055 ZZ9999997 00500011 XYZ DC SALT LAKE CITY 0050001131 20171014 0922 XY9999997 20171014 00999111101 20171014 0926751452 20171012 1 44.7 SUPLYY DEPTSTRE #9 9900 WEST SOMEWHER AVENUE NAMPA ID 83687 EA 990003055 ZZ9999998 00500011 YZZ DC SACRAMENTO 0050001131 20171014 0922 YZ9999998 20171014 00999112501 20171014 0926516685 20171007 1 62.45 Lst East SUPPL CH SRV EDI #99 9920 West EntrtyPLEX DR. VISALIA CA 93291-623 EA 990003055 ZZ9999998 00500011 YZZ DC SACRAMENTO 0050001131 20171014 0922 YZ9999998 20171014 00999112501 20171014 0926678606 20171011 1 62.45 Lst East SUPPL CH SRV EDI #99 9920 West EntrtyPLEX DR. VISALIA CA 93291-623 EA 990003055 ZZ9999998 00500011 YZZ DC SACRAMENTO 0050001131 20171014 0922 YZ9999998 20171014 00999112501 20171014 0926515844 20171007 2 124.9 SUPLYY DEPTSTRE CHAN #9 9900 E SOMEWHR AVENUE FRESNO CA 93720 EA 990003055 ZZ9999999 00500011 ZZZ DC ORLANDO VISTA 0050001131 20171014 0922 YZ9999998 20171014 00999113401 20171014 0926715863 20171012 3 256.5 SUPLYY DEPTSTRE CHAIN #9 LLC 900 SOMEWHR, STE 9 ESTON MIAMI FL 33126 EA 990003055 ZZ9999999 00500011 ZZZ DC ORLANDO VISTA 0050001131 20171014 0922 ZZ9999999 20171014 00999111101 20171014 0926548404 20171009 1 44.7 SUPPLY DEPTSTRE CHAINS LLC 900 SOMEWHER AVE MIAMI FL 33155 EA 990003055 ZZ9999999 00500011 ZZZ DC ORLANDO VISTA 0050001131 20171014 0922 XY9999997 20171014 00999113201 20171014 0926712287 20171012 2 110.1 Lst East SUPPL CH SRV EDI #9 9920 West EntrtyPLEX RD OCOEE FL 34761 EA 990003055
- Top
- Bottom
Comment
-
Originally posted by MoonRise View PostCorrect, some line could have extra white spaces and are necessary, one reason the file is TAB delimited. However, as you see with Column 3 it is totally blank BUT that column has to exist and I don't care if it has something or not. When we get to Column 6 is where I do care if something is there. If a row at column 6 is blank I don't want to include it in the output. Hope that explains better.
The example file you provided doesn't have any lines where column 6 contains 'no data'. So that I'm crystal clear (a necessary condition!), column 6 (in every line provided) contains the value 20171014. Is that correct?Windows no longer obstructs my view.
Using Kubuntu Linux since March 23, 2007.
"It is a capital mistake to theorize before one has data." - Sherlock Holmes
- Top
- Bottom
Comment
-
From my brother-in-law:
change awk command line
from ... 'if($6 != ...
to ... 'if(\$6 != ...
escape the dollar sign ($) so that the command passed to awk by the shell passes the literal $6, instead of the shell's substitution of the sixth argument on the bash command line.
Alternatively, put the awk command(s) in a separate file for awk to execute, rather than passing the awk command(s) on the command line itself. Use an editor (not the bash "echo" command) to build this awk script file.
Assuming the file "myFl.awk" contains the line (created via an editor, (ie, vi):
{if($6 != "") {print}}
Then the bash command becomes:
awk -F $'\t' -f myFl.awk $myFl >> $myFlfWindows no longer obstructs my view.
Using Kubuntu Linux since March 23, 2007.
"It is a capital mistake to theorize before one has data." - Sherlock Holmes
- Top
- Bottom
Comment
-
Well; the first part I've done that and get the below:
Code:awk: cmd. line:1: {if(\$6 != "") {print}} awk: cmd. line:1: ^ backslash not last character on line awk: cmd. line:1: {if(\$6 != "") {print}} awk: cmd. line:1: ^ syntax error
- Top
- Bottom
Comment
-
Yeah, we figured that out as well.
Try this modified script:
Code:#! /bin/sh # cd /home/moonrise/Documents/My_Work/fakeServer/delivEDI/dropEDI for file in /home/moonrise/Documents/My_Work/fakeServer/delivEDI/dropEDI/*; do myDATE=`date '+%Y%m%d'` myTIME=`date '+%H%M%S'` myFl="EDI_file_${myDATE}${myTIME}.TXT" myFlf="EDI_file_f_${myDATE}${myTIME}.TXT" mv ${file##*/} "/home/moonrise/Documents/My_Work/fakeServer/delivEDI/dropEDI/$myFl" echo $myFl sed -i '/BLINDED/d' $myFl done awk -F $'\t' '{$6 != ""}' $myFl >> $myFlf
Last edited by Snowhog; Nov 19, 2018, 04:21 PM.Windows no longer obstructs my view.
Using Kubuntu Linux since March 23, 2007.
"It is a capital mistake to theorize before one has data." - Sherlock Holmes
- Top
- Bottom
Comment
-
That is odd then. Still get an empty file here. Maybe there is something with konsole itself then. I guess I'll move it to another system I have and see what it does there. I've made no modifications to konsole other than color of text.
- Top
- Bottom
Comment
-
In order to 'test' the script, I made 'some' changes (your directories don't exist at here).
The "modified" script I tested, and that worked with the provided .txt file, is:
Code:#! /bin/sh # for file in /User/paul/dropEDI/*; do myDATE=`date '+%Y%m%d'` myTIME=`date '+%H%M%S'` myFl="EDI_file_${myDATE}${myTIME}.TXT" myFlf="EDI_file_f_${myDATE}${myTIME}.TXT" mv ${file##*/} "/User/paul/dropEDI/$myFl" echo $myFl #sed -i '/BLINDED/d' $myFl done awk -F $'\t' '{$6 != ""}' $myFl >> $myFlf
Added:
What is the name of the 'original' source file that the script runs against?
What are the names of the source files that this script is reading? I'm skeptical as to the myFl= and myFlf= variable creation lines. Are the source files named EDI_file_ and EDI_file_f_ or something else?Last edited by Snowhog; Nov 19, 2018, 06:05 PM.Windows no longer obstructs my view.
Using Kubuntu Linux since March 23, 2007.
"It is a capital mistake to theorize before one has data." - Sherlock Holmes
- Top
- Bottom
Comment
-
We got it working.
Compare this slightly modified script that we tested on your provided file. That is, got it working on my iMac with it's bash.
Code:#! /bin/sh # cd /home/moonrise/Documents/My_Work/fakeServer/delivEDI/dropEDI for file in /home/moonrise/Documents/My_Work/fakeServer/delivEDI/dropEDI/*; do myDATE=`date '+%Y%m%d'` myTIME=`date '+%H%M%S'` myFl="EDI_file_${myDATE}${myTIME}.TXT" myFlf="EDI_file_f_${myDATE}${myTIME}.TXT" mv ${file##*/} "/home/moonrise/Documents/My_Work/fakeServer/delivEDI/dropEDI/$myFl" sed -e '/BLINDED/d' -i "" $myFl done awk -F $'\t' '$6 != ""' $myFl >> $myFlf
Last edited by Snowhog; Nov 19, 2018, 07:54 PM.Windows no longer obstructs my view.
Using Kubuntu Linux since March 23, 2007.
"It is a capital mistake to theorize before one has data." - Sherlock Holmes
- Top
- Bottom
Comment
Comment