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
A question/problem about oracle "tns listener" and "enterprise manager" talipk UNIX and Linux Applications 0 12-01-2008 03:08 PM
"|" separated file validations kolesunil Shell Programming and Scripting 1 05-27-2008 06:19 AM
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 03:00 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 04:15 AM

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 01-26-2009
shira's Avatar
shira shira is offline
Registered User
  
 

Join Date: Jan 2009
Location: Israel
Posts: 35
How to cut a file using " ", but fields can be separated with more than one " "

Hello,

let's say I have a text file:

Code:
 
word11 word12 word13
word21 word22 word23
word31 word32 word33

and I want to put the second field of each line into a list:

Code:
set list = `cut -d" " -f2 ${1}`

and I use space (" ") as a delimiter, only that there's a catch:
there can be more than one space between two words, for example:


Code:
 
word11 word12    word13
word21    word22 word23
word31 word32 word33

how do I ignore the multiple spaces, and make sure that:
list == {word12 word22 word32}

thanks,
Shira.
  #2 (permalink)  
Old 01-26-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,800

Code:
set list = `awk {print $2}' ${1}`

try awk
  #3 (permalink)  
Old 01-26-2009
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 use awk, or tr (to get rid of duplicate spaces)


Code:
> echo "word1 word2 word3" | cut -d" " -f2
word2
> echo "word1  word2 word3" | cut -d" " -f2

> echo "word1  word2 word3" | tr -s " " | cut -d" " -f2
word2
> echo "word1  word2 word3" | awk '{print $2}'         
word2

The tr -s will suppress repeated consecutive characters; thus eliminating the extra space characters.
Awk natively separates on space (one or more) or tab characters.
  #4 (permalink)  
Old 01-26-2009
shira's Avatar
shira shira is offline
Registered User
  
 

Join Date: Jan 2009
Location: Israel
Posts: 35
Thanks!
I was wondering if there's a solution that involves perhaps a pipeline or a loop instead of an awk?
I thought about

Code:
set list = `cat ${1} | cut -d" " -f3`

but it didn't work...
  #5 (permalink)  
Old 01-26-2009
shira's Avatar
shira shira is offline
Registered User
  
 

Join Date: Jan 2009
Location: Israel
Posts: 35
Hey Joey, thanks so much!

how do I write it (with tr) in my script?
like this?


Code:
set list = `tr -s " " | cut -d" " -f2 ${1}`

or like this:


Code:
set list = `cat ${1} | tr -s " " | cut -d" " -f2`

or in a different way?
And what is the tr function anyway? what does it do?
  #6 (permalink)  
Old 01-26-2009
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


Code:
> cat file146
val1 val2 val3
val4  val5 val6
val7   val8  val9

> tr -s " " <file146 | cut -d" " -f2
val2
val5
val8

> tr -s " " <file146 | cut -d" " -f2 >file146.out
> cat file146.out
val2
val5
val8

OR....


Code:
> mylist=`tr -s " " <file146 | cut -d" " -f2`
> echo ${mylist}
val2 val5 val8

  #7 (permalink)  
Old 01-26-2009
shira's Avatar
shira shira is offline
Registered User
  
 

Join Date: Jan 2009
Location: Israel
Posts: 35
OK, I read about it, tr is a genial command!
It worked!
Joey, you're the best, thanks so much!
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 08:39 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