The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to convert Fixed length file to delimited file. satyam_sat Shell Programming and Scripting 7 04-03-2008 02:41 AM
Converting a Delimited File to Fixed width file raghavan.aero Shell Programming and Scripting 2 06-06-2007 02:44 PM
Converting Tab delimited file to Comma delimited file in Unix charan81 Shell Programming and Scripting 22 01-20-2006 09:24 AM
tab delimited file to commas hcclnoodles Shell Programming and Scripting 6 08-16-2004 01:23 PM
Comma Delimited file dbrundrett Shell Programming and Scripting 2 04-05-2004 10:50 AM

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

Join Date: Mar 2008
Posts: 4
Delimited File Help

Hey guys I am new to unix programming and need some help with a task.

I have a file that is comma delimited with values like this

1,NEW.txt
1,OLD.txt
2,MONKEY.txt
3,FUN.txt
100,MONEY.txt

I have a user enter a value for the type they want (1,2,3,100).

I then want to compare the first column in the file to that entered value and return the second column in that file if it matches.

If 1 is entered I want returned:

NEW.txt
OLD.txt

I cannot figure out how to use awk, cut or sed to do this.

Any help would be greatly appreciated. Thanks in advance folks!
  #2 (permalink)  
Old 03-27-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
One way of doing it:

script

Code:
#!/bin/bash
filename="$1"
echo "Enter value "
read value

awk -F, -v var="$value"  '{if (var == $1 ) print $2}'  "$filename"
It should be run as :

Code:
./script your_file
  #3 (permalink)  
Old 03-28-2008
garpal garpal is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 4
Rubin... I am getting an error on the code given. I hardcoded some test data and tried to run.

Code:
awk -F, -v var="1"  '{if (var == $1 ) print $2}'  GO_FILE_LIST.dat   

awk: syntax error near line 1
awk: bailing out near line 1
  #4 (permalink)  
Old 03-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Works here. Do you have a really old version of awk? The script itself is so trivial as to hardly contain any syntax at all -- does your awk understand -F and -v? If not, do you have access to nawk, or mawk, or gawk, ... or (gasp) Perl? (Hint: a2p)
  #5 (permalink)  
Old 03-28-2008
garpal garpal is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 4
Thats a very good question, I have no idea what version of awk I have...

I have access to nawk but not the other two. And I do have access to Perl but know nothing about it.


This ended up working for me... Can you see any issues with this?

Code:
$VAR_NAM=$1
$FILE_LIST=$2
$TEMP_FILE=$3

awk -F, '$1=='"$VAR_NAM"' {print $2}'  $FILE_LIST > $TEMP_FILE
Thanks for the help!

Update...
Using nawk the previous code worked fine with the IF statements thanks for the help folks!
  #6 (permalink)  
Old 03-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Strictly speaking, you should put double quotes around the file names too. And obviously, when assigning to a variable, you don't put a dollar sign in front of the variable name. Other than that, schoolbook example -- good work.

Would still be interesting to learn if switching from awk to nawk would solve the problem you were having with the syntax error.
Closed Thread

Bookmarks

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 06:48 AM.


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