Hi,
How can I pop the first line of a file and affect in to a variable ?
example file:
line1
line2
line3
command:
my_var=cmd
results:
my_var=="line1"
file==
line2
line3
Thanks.
Fred
How can I pop the first line of a file and affect in to a variable ?
example file:
line1
line2
line3
command:
my_var=cmd
results:
my_var=="line1"
file==
line2
line3
Thanks.
Fred
> example file:
> line1
> line2
> line3
> command:
> my_var=cmd
> results:
> my_var=="line1"
> file==
> line2
> line3
#! /bin/bash
pop()
{
counter=0
ret=
while read line;do
[[ counter -eq 0 ]] && ret=$line || echo $line
(( counter += 1 ))
done < $1 > $1.tmp
mv $1.tmp $1
echo $ret
my_var=`pop dat`Quote:}
Joe
> Hi,
> How can I pop the first line of a file and affect in to a variable ?
> example file:
> line1
> line2
> line3
> command:
> my_var=cmd
> results:
> my_var=="line1"
> file==
> line2
> line3
> Thanks.
> Fred
Would be 1 way.
AC
> How can I pop the first line of a file and affect in to a variable ?
> example file:
> line1
> line2
> line3
> command:
> my_var=cmd
> results:
> my_var=="line1"
> file==
> line2
> line3
Example:
pop () {
# Print only first line of file:
head -1 "$1"
# Rewrite file, omitting first line:
sed -n '2,$p' "$1" > $$ && mv $$ "$1"
}
firstline=`pop file`
Heiner
--
___ _
\__ \ _/ -_) V / -_) ' \ Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_| http://www.shelldorado.com/
> How can I pop the first line of a file and affect in to a variable ?
> example file:
> line1
> line2
> line3
> command:
> my_var=cmd
> results:
> my_var=="line1"
> file==
> line2
> line3
> Thanks.
> Fred
or if your version of sed doesn't support -i, use a tmp file (and then
you could just use "tail +2 file" instead of sed) or switch to ed:
read my_var < file
ed -s file <<!
1d
w
q
!
Regards,
Ed.
>>example file:
>>line1
>>line2
>>line3
>>command:
>>my_var=cmd
>>results:
>>my_var=="line1"
>>file==
>>line2
>>line3
This prints the first line of the file, but it does notQuote:> var=`sed -n '1p'`
> Would be 1 way.
Additionally,
sed -n '1{p;q;}'
would be more efficient, because this way "sed" would stop
reading the file after the first line. Otherwise it would
continue reading (and ignoring) every single line of the
input file until the very end of it ;-)
Speed difference:
1000 runs on /usr/share/dict/words (Linux):
$ wc /usr/share/dict/words
45378 45378 408865 /usr/share/dict/words
### sed -n '1p'
1000 runs real user sys
total 8.10 6.78 0.71
### sed -n '1{p;q;}'
1000 runs real user sys
total 0.83 0.29 0.47
Heiner
--
___ _
\__ \ _/ -_) V / -_) ' \ Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_| http://www.shelldorado.com/
> Speed difference:
Whew! Big difference. Thanks Heiner. An important principle there.Quote:> 1000 runs on /usr/share/dict/words (Linux):
> $ wc /usr/share/dict/words
> 45378 45378 408865 /usr/share/dict/words
> ### sed -n '1p'
> 1000 runs real user sys
> total 8.10 6.78 0.71
> ### sed -n '1{p;q;}'
> 1000 runs real user sys
> total 0.83 0.29 0.47
> Heiner
So to do what the OP wants, he needs to run that sed script to get the
variable loaded and
echo -e '1d\nwq\n' | ed -s inputfile
to remove the first line efficiently.
Or would a HERE document be better?
AC
Try:Quote:> Hi,
> How can I pop the first line of a file and affect in to a variable ?
> example file:
> line1
> line2
> line3
> command:
> my_var=cmd
> results:
> my_var=="line1"
> file==
> line2
> line3
#!/bin/bash
lines=1;
fname='yourfile';
line=`head -n$lines $fname`;
echo $line;
exit 0;
I hope this helps.
Anthony Borla
> > How can I pop the first line of a file and affect in to a variable ?
<SNIP>
Anthony Borla
> How can I pop the first line of a file and affect in to a variable ?
> example file:
> line1
> line2
> line3
> command:
> my_var=cmd
> results:
> my_var=="line1"
> file==
> line2
> line3
read var < FILE
Depending on the contents of the file, you might want (or need) to
change IFS (to retain leading or trailing spaces) and/or use the
raw flag (-r; to prevent interpreting backslash sequences).
If you also want to remove the first line from the file:
tail +2 FILE > tempfile
mv tempfile FILE
--
Chris F.A. Johnson http://cfaj.freeshell.org
===================================================================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
[...]Quote:> To read the first line of a file into a variable:
> read var < FILE
> Depending on the contents of the file, you might want (or need) to
> change IFS (to retain leading or trailing spaces) and/or use the
> raw flag (-r; to prevent interpreting backslash sequences).
It would make more sense to say it the other way round:
To read the first line of a file into a variable:
IFS= read -r var < FILE
If you need the leading and trailing blanks to be removed,
omit the "IFS="; if you want `\' to be treated as a special
character that allows to have a logical line splitted on
several physical line, omit the "-r".
Note that "read" only works on text input (fails on NULs, and
the result is unspecified if there are characters past the last
\n is the file).
--
Stphane ["Stephane.Chazelas" at "free.fr"]
>> read var < FILE
>> Depending on the contents of the file, you might want (or need) to
>> change IFS (to retain leading or trailing spaces) and/or use the
>> raw flag (-r; to prevent interpreting backslash sequences).
> [...]
> It would make more sense to say it the other way round:
This does not work in Bourne shells.Quote:> To read the first line of a file into a variable:
> IFS= read -r var < FILE
90% of the time (if not more) the reason for reading a line into aQuote:> If you need the leading and trailing blanks to be removed,
> omit the "IFS="; if you want `\' to be treated as a special
> character that allows to have a logical line splitted on
> several physical line, omit the "-r".
True, and that is usually understood. Unix utilities (and shells)Quote:> Note that "read" only works on text input (fails on NULs, and
> the result is unspecified if there are characters past the last
> \n is the file).
--
Chris F.A. Johnson http://cfaj.freeshell.org
===================================================================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Note that in early Bourne shells, evenQuote:>> IFS= read -r var < FILE
> This does not work in Bourne shells.
So if you want to stick with every Bourne shell compatibility,
you should not use read on a file.
Or use something like
{
read var
process "$a"
but beware that a subshell may be created for the {...}.Quote:} < FILE
Still, you can't say that
read var < FILE
puts the first line of FILE into $var unless you provide another
definition for "line" different from the one commonly accepted
(the characters from the beggining of the file until the first
occurrence of a NL character).
The OP didn't ask for how to get "the information stored in the
first logical line of a file (where logical line are made of
physical line continued on \\\n, and any other \ not followed by
\ removed)".
Note that there used to exist a "line" utility dedicated to this
(while read was the utility to read user input).
exec 3< file
var=`line <&3`
# note that "line" is not equivalent to "head -n 1" as "line"
# reads one character at a time just like "read", so that you
# can use it again to get the second line. "line" is deprecated
# since "read -r" exists
# (www.opengroup.org/onlinepubs/007904975/utilities/read.html)
Note that "rc", a much more consistent shell than Bourne like
ones has no "read" utility or equivalent.
--
Stphane ["Stephane.Chazelas" at "free.fr"]
However, concerning portability in practice, it makes senseQuote:> in early Bourne shells [...] you can't redirect builtins
> So if you want to stick with every Bourne shell compatibility,
> you should not use read on a file.
However, concerning portability in practice, it makes senseQuote:> in early Bourne shells [...] you can't redirect builtins
> So if you want to stick with every Bourne shell compatibility,
> you should not use read on a file.
1. how to append one file's line to another file's line?
I am not very farmiliar with UNIX. Now I met a problem. I want join two files
text line together. That's :
line1 from file1 line1 from file2
line2 from file1 line2 from file2
.
.
.
I don't know if I can do this through shell scripts. the questions is I don't
know how to process another file in a shell scripts.
Thank you,
Bill.
2. permissions on a mounted drive
3. Lines from a file, one-by-one
4. LiveWire 10/100 PCMCIA supported?
5. Content of file is one line has to be displayed in seperated lines
7. Insert a line at the first line without a # in a file
9. Delete the first line of a 1.4 million lines file
10. Lines from a file, one-by-one
11. one liner which prints n lines before and m lines after the line found by grep
12. looking for email client that supports one then one pop server !