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
use awk to read variable length csv shweta_d Shell Programming and Scripting 3 06-18-2007 02:16 AM
Fixed Length records- Korne Shell Program. nrajesh_2009 Shell Programming and Scripting 4 04-11-2007 12:41 PM
Select records based on search criteria on first column shashi_kiran_v UNIX for Dummies Questions & Answers 2 12-02-2005 01:49 PM
Length of a variable karyn1617 Shell Programming and Scripting 3 02-08-2005 06:41 PM
creating a fixed length output from a variable length input r1500 Shell Programming and Scripting 2 12-03-2003 01:09 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 08-21-2008
synergy_texas synergy_texas is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 19
Using awk to search variable length records

New to awk and need some help. I have a script that I would like to make more compact. I want to read a file and grab every field, from every record, except the last field. The records are variable length and have varying number of fields. A record will have at least two fields, but can have many more. Here is what I am currently using. Can someone provide a compact solution? Thanks.

awk '{ print $1 }' sftplist.txt >> users.txt
awk '{ print $2 }' sftplist.txt >> users.txt
awk '{ print $3 }' sftplist.txt >> users.txt
awk '{ print $4 }' sftplist.txt >> users.txt
awk '{ print $5 }' sftplist.txt >> users.txt
awk '{ print $6 }' sftplist.txt >> users.txt
awk '{ print $7 }' sftplist.txt >> users.txt
awk '{ print $8 }' sftplist.txt >> users.txt
awk '{ print $9 }' sftplist.txt >> users.txt
awk '{ print $10 }' sftplist.txt >> users.txt

This gives me what I want, but then I have a lot of cleanup after it, like removing blank lines and lines that contained the last field when I didn't want the last field.
  #2 (permalink)  
Old 08-21-2008
sudhamacs sudhamacs is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 98
rev sftplist.txt | cut -f2- | rev > users.txt
  #3 (permalink)  
Old 08-21-2008
synergy_texas synergy_texas is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 19
Thanks for the suggestion, but your solution still provides me the last field of the record of which I have to cleanup from the file. It's not that big of a deal to perform the cleanup, I just like to keep my scripts as compact as possible and not perform unnecessary processing.
  #4 (permalink)  
Old 08-21-2008
sudhamacs sudhamacs is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 98
Any sample input and output?
  #5 (permalink)  
Old 08-21-2008
synergy_texas synergy_texas is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 19
Sure,
Anything to help.

Here is sftplist.txt

user3 user1 user2 /mydir/next/dir1
user2 /mydir/next/dir2
user1 user4 /staging/next/dir1


Output users.txt

user3
user1
user2
user2
user1
user4

Hope this helps. I can manage the sort -u afterwards.
  #6 (permalink)  
Old 08-21-2008
sudhamacs sudhamacs is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 98
rev sftplist.txt | cut -d' ' -f2- | rev | tr ' ' '\n' > users.txt
  #7 (permalink)  
Old 08-21-2008
synergy_texas synergy_texas is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 19
Here is what I have,

rev sftplist.txt | cut -d ' ' -f2- | rev | tr ' ' '\n' > users.txt

This is what I get.

tr: 0653-712 The combination of options and String parameters is not legal.
Usage: tr [ -[c|C] | -[c|C]ds | -[c|C]s | -ds | -s ] [-A] String1 String2
tr { -[c|C]d | -[c|C]s | -d | -s } [-A] String1

Last edited by synergy_texas; 08-21-2008 at 06:51 PM..
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 09:43 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