Popping first line of a file

Popping first line of a file

Post by Frédéric Mayo » Thu, 15 Jan 2004 05:14:38



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

 
 
 

Popping first line of a file

Post by j.. » Thu, 15 Jan 2004 05:48:33



> 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

One possibility (minus error checking):

#! /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

Quote:}

my_var=`pop dat`
echo my_var = $my_var

Joe

 
 
 

Popping first line of a file

Post by Alan Conno » Thu, 15 Jan 2004 05:58:37



> 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

var=`sed -n '1p'`

Would be 1 way.

AC

 
 
 

Popping first line of a file

Post by Heiner Steve » Thu, 15 Jan 2004 06:25:34



> 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

The difficult part is to "pop" a line from a file, because
in order to do this you'll have to rewrite the file (without
the first line).

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/

 
 
 

Popping first line of a file

Post by Ed Morto » Thu, 15 Jan 2004 06:34:10



> 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

read my_var < file
sed -i '1d' file

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.

 
 
 

Popping first line of a file

Post by Heiner Steve » Thu, 15 Jan 2004 06:57:00




>>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

[...]

Quote:> var=`sed -n '1p'`

> Would be 1 way.

This prints the first line of the file, but it does not
remove it from the file. A second "pop" would get the
same first line again.

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/

 
 
 

Popping first line of a file

Post by Alan Conno » Thu, 15 Jan 2004 07:37:36





> Speed difference:

<snip>

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

Whew! Big difference. Thanks Heiner. An important principle there.

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

 
 
 

Popping first line of a file

Post by Anthony Borl » Thu, 15 Jan 2004 12:32:07



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

Try:

#!/bin/bash

lines=1;
fname='yourfile';
line=`head -n$lines $fname`;
echo $line;

exit 0;

I hope this helps.

Anthony Borla

 
 
 

Popping first line of a file

Post by Anthony Borl » Thu, 15 Jan 2004 16:00:06





> > Hi,

> > How can I pop the first line of a file and affect in to a variable ?

<SNIP>

Should've known the answer was *too* easy to be true. Ahh - how I just
*hate* misinterpreting posts / specs !!!

Anthony Borla

 
 
 

Popping first line of a file

Post by Chris F.A. Johnso » Fri, 16 Jan 2004 02:45:46



> 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

   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).

   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

 
 
 

Popping first line of a file

Post by Stephane CHAZELA » Fri, 16 Jan 2004 03:41:28


2004-01-14, 17:45(+00), Chris F.A. Johnson:
[...]
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"]

 
 
 

Popping first line of a file

Post by Chris F.A. Johnso » Fri, 16 Jan 2004 04:53:56



> 2004-01-14, 17:45(+00), Chris F.A. Johnson:
> [...]
>>    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:

    I disagree.

Quote:>    To read the first line of a file into a variable:

> IFS= read -r var < FILE

    This does not work in Bourne shells.

Quote:>    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".

    90% of the time (if not more) the reason for reading a line into a
    variable is to obtain information from the line. Leading and
    trailing (and often interspersed multiple) spaces are formatting,
    not information. Reading the entire line just means further,
    unnecessary operations to remove the dross.

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).

    True, and that is usually understood. Unix utilities (and shells)
    are for the most part designed to work with text files.

--
    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

 
 
 

Popping first line of a file

Post by Stephane CHAZELA » Fri, 16 Jan 2004 07:22:31


2004-01-14, 19:53(+00), Chris F.A. Johnson:
[...]

Quote:>> IFS= read -r var < FILE

>     This does not work in Bourne shells.

Note that in early Bourne shells, even
read var < FILE
doesn't work as you can't redirect builtins there.

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"

Quote:} < FILE

but beware that a subshell may be created for the {...}.

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"]

 
 
 

Popping first line of a file

Post by Sven Maschec » Fri, 16 Jan 2004 18:59:01



> 2004-01-14, 19:53(+00), Chris F.A. Johnson:
>>> IFS= read -r var < FILE
>> This does not work in Bourne shells.

(except the SVR4.2 shell)

Quote:> 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 sense
to stick with the SVR2 shell ff., which can do it.
 
 
 

Popping first line of a file

Post by Sven Maschec » Fri, 16 Jan 2004 19:21:44



> 2004-01-14, 19:53(+00), Chris F.A. Johnson:
>>> IFS= read -r var < FILE
>> This does not work in Bourne shells.

(except the SVR4.2 shell)

Quote:> 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 sense
to stick with the SVR2 shell ff., which can do it.
 
 
 

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

6. IP assignments

7. Insert a line at the first line without a # in a file

8. Function name in ksh

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 !