The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
reading text file line by line MizzGail Shell Programming and Scripting 6 04-14-2008 07:58 AM
Line by line file reading... and more! ProFiction Shell Programming and Scripting 6 07-26-2007 12:32 PM
Reading line by line from a file tej.buch Shell Programming and Scripting 2 01-23-2006 02:50 AM
sed not outputting last line of input file 2reperry Shell Programming and Scripting 3 12-16-2005 12:51 PM
Reading line by line from file. akpopa UNIX for Dummies Questions & Answers 4 08-30-2001 11:20 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-17-2008
Darkness Fish Darkness Fish is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 2
Question Problem with reading file line-by-line, and outputting to a new file

Hi everyone. I realise this is probably a bit of a noob question, but I'm actually a C# developer working on a legacy system, and can't remember much unix.

I want to read from a pipe-delimeted file like formatted thusly:

idno|PRODUCT|Name|street town postcode|etc|etc|etc|etc
idno|PRODUCT|Name|street town postcode|etc|etc|etc|etc
idno|PRODUCT|Name|street town postcode|etc|etc|etc|etc

I want to copy each line to another file, dependant upon the PRODUCT existing in a string variable I'm holding in memory: "prod1|prod2|prod3" ($products in example below)

Therefore I was trying:

for line in `cat $temp_dir/$Filename | awk '{ print $0 } '`
do
prod=`echo "$line" | awk ' BEGIN {FS="|"} {print $2} '`
valid_prod=`echo $products | grep "$prod"`
if [ -n "$valid_prod" ] ; then
echo "$line" >> $new_file.txt
fi
done

however this line variable seems to separate based on spaces, creating the new file as:

idno|PRODUCT|Name|street
town
postcode|etc|etc|etc|etc

Is there anyway I can pass each line through as a single parameter, regardless of spaces?

Any help is greatfully appreciated,
Dave
  #2 (permalink)  
Old 07-17-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink A different approach (albeit untested)


Code:
while read zf
   do
   prod=$(echo "$zf" | cut -d"|" -f2)
   valid_prod=$(echo "$products" | grep "$prod")
   if [ -n "$valid_prod" ]
      then
      echo "$zf" >>$new_file
   fi
done <$filename

Sometimes I get so confused using the ' and ` thus I prefer the above approach.
  #3 (permalink)  
Old 07-17-2008
TinWalrus TinWalrus is offline
Registered User
  
 

Join Date: May 2007
Posts: 35
Ifs

you can manipulate the IFS (internal field separator) - as an example - given that go.dat contains the lines with spaces

# create the array, without manipulating IFS, spaces will break the array
#
set -A lines $(cat go.dat)
for line in "${lines[@]}"; do
echo "'$line'"
done

# create the array, but first change the IFS (set it back - very important)
# after the array is set
#
oIFS=$IFS
IFS=${IFS##?}
set -A lines $(cat go.dat)
IFS=$oIFS

for line in "${lines[@]}"; do
echo "'$line'"
done


### go.dat ###
idno a|PRODUCT|Name|street town postcode|etc|etc|etc|etc
idno b|PRODUCT|Name|street town postcode|etc|etc|etc|etc
idno c|PRODUCT|Name|street town postcode|etc|etc|etc|etc

### end go.dat ###


i realize that does not help your problem (darkness fish already answered) so i thought i would throw my .02 in about IFS - since i have been bitten by that in the past - and it comes in handy quite often
  #4 (permalink)  
Old 07-18-2008
Darkness Fish Darkness Fish is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 2
Thanks very much for your help, both of you, it's greatly appreciated.

I was getting lost in a world of random unintelligible awk commands found via google.
  #5 (permalink)  
Old 07-18-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool In unix, there are an nearly infinite number of ways to solve things

As you read through this forum, you should get a flavor of the different ways to solve a problem. There are issues of
awk vs grep or cut
useless cat's
and many others. Some solutions may be more robust, while others just look good.

So, don't belittle poor little awk - it serves great purposes for certain database problems.
Closed Thread

Bookmarks

Tags
awk, file, grep or, read, spaces, write

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0