ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
i believe my regex was taking (^) as a literal, need to verify
If that is true then you need to escape it, otherwise it will be seen as at the beginning of the string and consequently all lines in the file are shown.
With looking for a literal ^ also included, the command becomes:
If I am following the last example I assume you wish to concatenate all remaining fields into the variable last?
If so then simply place the FS between items, assuming you still wish to use it, as + is for arithmetic:
Code:
for (i=7; i<=NF; i++)
last = last FS $i
print $1,$2,last
Also note that last is a string variable and not a field identifier so the $ is not needed.
i do something like this, was looking to make it a tad more elegant
Code:
#!/bin/bash
awk '
BEGIN {
OFS=",";
}
{
if ( NF == 0 || $1 ~ /^(aaa|bbb|-|+|=|0$|1\/|\/\/|TPP|PASSWORD|DYNAMIC|1E)/ ) {}
else {
last = $6$7$8$9$10$11$12$13
print $1,$2,$3,$4,$5,last;}
} ' | sed 's/^0\(.*\)/\1/'
i execute like this
./script.awk < infile > outfile
i also need to add regex for mm/dd/yy in the If statement, but having a hard time as \d{2}/\d{2}/\d{2} fails
should i use [:digit:]\{2\}\/[:digit:]\{2\}\/[:digit:]\{2\}
Last edited by Linux_Kidd; 04-18-2012 at 02:53 PM.
If I am following the last example I assume you wish to concatenate all remaining fields into the variable last?
If so then simply place the FS between items, assuming you still wish to use it, as + is for arithmetic:
Code:
for (i=7; i<=NF; i++)
last = last FS $i
print $1,$2,last
Also note that last is a string variable and not a field identifier so the $ is not needed.
If I am on the wrong path you can ignore me
all helps a lot. i dont need FS in between, i need a space.
The default FS is a space. Using FS instead of a real space make the script more flexible. If you ever need to change the FS to something other then a space, you only need to change it once and do not have to search the complete script for relevant spaces that need changing.
When you create a small awk script (or one-liner) this isn't too relevant, but it is good practise nonetheless.
The default FS is a space. Using FS instead of a real space make the script more flexible. If you ever need to change the FS to something other then a space, you only need to change it once and do not have to search the complete script for relevant spaces that need changing.
When you create a small awk script (or one-liner) this isn't too relevant, but it is good practise nonetheless.
Hope this clears things up.
my bad, i thought i defined FS, but it was OFS. i will try with FS in there and test. too many things going on right now.
this small script processes a bunch of txt files, spits it out pipe delimited for Excel. script took 3sec to process the files. the files were previously processed manually by a person, that person estimated he spent approx 40 man hrs to process the files !!!
You don't happen to work in a bank by chance? I had a very similar experience where a process could only be run over the weekend as it took almost 40hrs to complete a single run. After I had been
there 3 months we were able to run it adhoc whenever we liked as it took about 3 minutes
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.