- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Unix reading lines from a file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2007 01:14 AM
07-04-2007 01:14 AM
I am trying to read a single line from a file and i am using the following code:
#!/bin/ksh
i=1
if [ $i -le 45 ]
then
sed -n ''$i','$ip'' a.dat>>c.dat
i=i+1
fi
Now somehow i am missing out the sed options.
My basic purpose of these steps is to merge the content of 30 files whose locations are in the file a.dat. Can some one chip in with some help
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2007 01:28 AM
07-04-2007 01:28 AM
Re: Unix reading lines from a file
# i=1; f=45
# sed -n "$i","$f"p a.dat > c.dat
...will write lines 1-45 of a.dat into c.dat
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2007 01:44 AM
07-04-2007 01:44 AM
Re: Unix reading lines from a file
Thanks...
Actually my fault i confused you with the post. The code which i have written is just to check if I am able to get the lines one by one written into c.dat...what i am looking ideally is for something which picks out each line one by one from a file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2007 01:55 AM
07-04-2007 01:55 AM
Re: Unix reading lines from a file
If all you want to do is read lines from a file, the shell can do this easily:
#!/usr/bin/sh
typeset -i LINENBR
while read LINE
do
(( LINENBR=LINENBR+1 ))
if [ "${LINENBR}" -le 45 ]; then
echo ${LINE} >> c.dat
fi
done < a.dat
exit 0
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2007 01:57 AM
07-04-2007 01:57 AM
Re: Unix reading lines from a file
I am trying to do this using sed the stream editor...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2007 02:03 AM
07-04-2007 02:03 AM
Re: Unix reading lines from a file
- to get exactly one specific line out of a file, I use
li=45
sed $li!d filename
- to walk through a file line by line (my example puts the line number as a prefix):
typeset -i num=0
while read li
do print L$((num+=1)):$li
done
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2007 02:05 AM
07-04-2007 02:05 AM
Re: Unix reading lines from a file
> I am trying to do this using sed the stream editor...
That's wasteful if all that you are doing is using 'sed' to specify a line number and the line numbers represent a continuum. You are forcing 'sed' to read the input file n-times!
Hoever, your original code should have been:
# i=1;f=45;sed -n "$i","$f"p a.dat
...or for any line "i":
# sed -n "$i"p filein >> fileout
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2007 02:13 AM
07-04-2007 02:13 AM
Re: Unix reading lines from a file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2007 02:15 AM
07-04-2007 02:15 AM
Solutionto respond to your overlapping post:
> I am trying to do this using sed the stream editor...
I wonder, why one should do it that way, but ...
typeset -i num=45 i=0
while [ i -le num ]
do
sed -e $((i+=1))!d -e q filename >c.dat
done
The 'q' command terminates the sed as a speedup on large files.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2007 02:19 AM
07-04-2007 02:19 AM
Re: Unix reading lines from a file
I see too, that you are a new member in our community. Welcome! Please see:
http://forums1.itrc.hp.com/service/forums/helptips.do?#28
Regards!
...JRF...