LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-27-2011, 01:04 PM   #1
boumphreyfr
LQ Newbie
 
Registered: Apr 2011
Distribution: lubuntu
Posts: 14

Rep: Reputation: 0
problem with ' set $(cat crpage)'


I have several lines of text that I want to sort by line and display
Code:
#!/bin/bash 
crpage="
                               NEW YORK
                         HENRY HOLT AND COMPANY
                                  1916
                       [several more lines...] 
                     J. R., L. E. W., and L. T. S.,
                  without whose help this small record
                      could not have been written."

	IFS=$'\n'
	set $(cat crpage) 
        echo $1
        echo $2
        echo $15
exit

The proble is $1 and $2 show the first and second ine but $15 shows:
NEW YORK5
In other words line 1 !!

Ie tried quotes with no success. Any ideas?

Last edited by boumphreyfr; 04-27-2011 at 04:26 PM. Reason: put in code tags
 
Old 04-27-2011, 01:26 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Positional parameters larger than 9 need to use the full variable form: "${15}".

And from now on, please use [code][/code] tags around your code, to preserve formatting and to improve readability.
 
1 members found this post helpful.
Old 04-27-2011, 03:01 PM   #3
boumphreyfr
LQ Newbie
 
Registered: Apr 2011
Distribution: lubuntu
Posts: 14

Original Poster
Rep: Reputation: 0
Thankyou
My first post didn't know about [code][code] ,should have RTFM
Frank
P.S.
I have now added code tags

Last edited by boumphreyfr; 04-27-2011 at 04:27 PM. Reason: announce:added code tags
 
Old 04-28-2011, 07:37 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Not to worry. It's common with newcomers.

Although I would've put the ending tag after the last line of code instead of the end of the post.

A couple more things to comment on:

You can usually use redirections instead of cat when accessing file contents. In this case you can use:
Code:
set $( <crpage )
Personally, I don't like using set/shift with the positional parameters. I prefer to use arrays instead. Bash v.4 even has a new mapfile built-in that makes it easy to load lines from a file into an array.
Code:
mapfile -t arr <crpage     #-t removes the trailing newline
     
#or for earlier versions of bash:
#IFS=$'\n'
#arr=( $(<crpage) )

echo "${arr[0]}"           #you should generally quote variable expansions
echo "${arr[1]}"           #especially when they can contain spaces
echo "${arr[14]}"          #and other shell-reserved characters
There's no need to fool with IFS this way either (at least, not with v.4). The only thing you have to remember is that arrays are 0-indexed, so you need to subtract one from each line number to reference it.

(Or mapfile also has an -O option, which lets you set the index start number to whatever you want.)
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cat 6 Cat 5e cable question babbab Linux - Networking 1 04-07-2010 02:33 PM
Using CAT / GREP to remove a set of lines from a file wesgarner Linux - Newbie 13 11-14-2009 10:41 AM
for loop cat problem jong357 Slackware 4 04-09-2007 12:05 PM
cat in asm/ cat --show-all option Tux Linux - Software 1 09-02-2006 09:31 PM
cat sh script wildcard problem scm Programming 8 04-21-2004 05:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration