AWK - Ignoring White Space with FS


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK - Ignoring White Space with FS
# 1  
Old 02-15-2012
AWK - Ignoring White Space with FS

I have an AWK script that uses multiple delimiters in the FS variable.
FS="[\. _]+"

My awk script takes a file name such as this:
12345_smith_bubba_12345_20120215_4_0.pdf and parses it out based on the under score. Each parsed field then has some code for data validation etc.

This script has been working fine until I got a file name like this:
12345_smith johnson jones_bubba_12345_20120215_4_0.pdf
Where the last name field has three last names separated by a space.

Is there a way in my FS assignment to tell AWK to ignore the whitespace and just use the period "." and underscore "_" as the only delimiters?

FYI this is my first post! Smilie

Randy
# 2  
Old 02-15-2012
Try:
Code:
FS="[\._]+"

# 3  
Old 02-15-2012
Otherwise you can try renaming the file with a character that is not listed in the possible FS example:

Code:
12345_smith-johnson-jones_bubba_12345_20120215_4_0.pdf

then run your script again

I am reluctant to suppress the space from the -F list , since if the coder added it to the list, maybe it is used to process some further step in the awk script. So if you remove it, you have to make sure that it has no impact on the remaining awk code.

Last edited by ctsgnb; 02-15-2012 at 11:45 AM..
# 4  
Old 02-15-2012
Are you on Solaris -try nawk with Bartus' answer.
# 5  
Old 02-15-2012
Bartus - That was the first thing I tried and still got the same result.

ctsgnb - The script's purpose is to create an "index" file used by an application to import PDF's for viewing. The application will have the person's name preloaded. So when it looks to the index file for the person's name, it has to be exact or it will fail to import. So the name has to maintain the spaces.

Thank you for replying so fast!

---------- Post updated at 10:44 AM ---------- Previous update was at 10:43 AM ----------

Jim - We are on HP UX.
# 6  
Old 02-15-2012
Where do you set the FS variable? Could you post the script or the relevant part of it?
# 7  
Old 02-15-2012
I set the FS in the BEGIN block:
Code:
BEGIN  {
 RS="\n"  # new_line record separator
 
 FS="[\. _]+"
 OFS="_"

Then in the main body of the script I do a print to an output file:
Code:
          field2=$2
          printf ("\"%s\",",field2)     >> path


Last edited by Franklin52; 02-16-2012 at 03:31 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing white space in awk

Hi How to remove white space from this input:|blue | 1| |green| 4| |black| 2| I like to search for green and get 4not 4 How to modify this to work correct:awk -F"|" '/green/ {print $3} (7 Replies)
Discussion started by: Jotne
7 Replies

2. Shell Programming and Scripting

awk - trim white space from a field / variable

Hi, Consider the data (FS = |): 1| England |end 2| New Zealand |end 3|Australia|end 4| Some Made Up Country |end 5| West Indies|end I want the output to be (i.e. without the leading and trailing white space from $2) England New Zealand Australia Some Made Up Country West... (4 Replies)
Discussion started by: Storms
4 Replies

3. Shell Programming and Scripting

Undesired removal of white space with awk

Hi, I'm fairly new to scripting and I have a problem that I am having difficulty solving. What I'd like to do is run an awk script to adjust the string in the first field depending on the string in another field. This is best explained with an example: Here is my script: cat... (4 Replies)
Discussion started by: calbrex
4 Replies

4. Shell Programming and Scripting

concatenate is ignoring space

Hi All,I am facing the below problem I have set a variable: a=`cat a.txt| grep "mad" | cut -c 30-50`the output is coming echo $a1 10 Mad300 3215however the actual ouput is 1.10 Mad300 3215There are 4spaces between 300 and 3215 so if i do: echo "$a" I am getting correct output: 1.10... (3 Replies)
Discussion started by: mad_man12
3 Replies

5. UNIX for Advanced & Expert Users

Tcl script for seaching a string ignoring white spaces

Hi , I want to search a string in a file ignoring white spaces in TCL. My string is as follows Ouput-Maps " 1 1 1 0 " 1i am doing following set a *1*1*1*0* " }1 abc.log}] but it is not working. What is the wrong with the tcl script Thanks Gouranga Video... (0 Replies)
Discussion started by: mybapa3000@gmai
0 Replies

6. Post Here to Contact Site Administrators and Moderators

Want a tcl script to compare a string in a file ignoring white spaces

Hi , I want a tcl script to search a string ignoring whitespaces in a .log file . It should correctly match . The string are as follows "Output-Maps 1 1 0 0 0" 1 and Active Intermediate-Maps 0 0 0 ... (1 Reply)
Discussion started by: kulua
1 Replies

7. Shell Programming and Scripting

awk: Eliminating white space while setting variable

Hi, I have a large flat file from host without delimiter. I'm transforming this file to a csv file using statements like # Row 03: Customer / field position 3059 +20 WOFABNAM=substr( $0, 3059, 20 ); and deleting the trailing whitespaces before and after with that sub( /^ +/, "",... (4 Replies)
Discussion started by: Celald
4 Replies

8. Shell Programming and Scripting

sed + white space

Hi, What sed command (if sed is the right command) can remove ALL white space from my file. I have a csv, except I want to remove all white space between commas and characters. My idea (without testing) sed 's/ //g' Is there a better way? (18 Replies)
Discussion started by: mcclunyboy
18 Replies

9. UNIX for Dummies Questions & Answers

SED with White Space

Dear Members, Suppose i have a variable test which stores a string as below: test='John drives+++++++++a+++++car' now i want to use sed on the above variable and replace + with a white space, so that i get echo $test should give me 'john drives a car' Between... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

10. UNIX for Dummies Questions & Answers

wc of characters in a file ignoring white space

Hi everyone, $ more abcdefg.ksh abcdef alpha beta gamma abcdef abcdef lmnop $ wc sachin1.ksh 5 7 132 abcdefg.ksh if you see it shows that file has got 240 characters. I actually want to count how many characters... (1 Reply)
Discussion started by: sachin.gangadha
1 Replies
Login or Register to Ask a Question