print specific line from multiple files

157

Solution 1

 awk 'FNR==27 {print FILENAME, $0}' *.txt >output.txt
  • FILENAME is built-in awk variable for current input file name
  • FNR refer to line number of current file
  • $0 means whole line

Solution 2

Another possibility is :

for i in * ; do echo -n $i" : "  ; head -n 27 "$i" | tail -n 1 ; done > output.txt

Solution 3

Using a for loop:

{ for i in *.txt; do echo "$i : $(sed -n '27p' "$i")"; done ;} >output.txt

The for loop may be expensive as you have 5000+ files but given the current hardwares should not be a problem.


Faster way, quitting sed after line 27 (thanks @Fiximan):

{ for i in *.txt; do echo "$i : $(sed -n '27p;q' "$i")"; done ;} >output.txt
Share:
157

Related videos on Youtube

Joey
Author by

Joey

Updated on September 18, 2022

Comments

  • Joey
    Joey over 1 year

    Here is the source code of a historical Unix goto.c: http://v6shell.org/history/goto.c

    So we're trying to find a matching label recording the "goto label" call.

    My problem:

    if (getlin(line)) {
            write(1, "label not found\n", 16);
            return;
            }
    

    So I would expect if getlin() returns true we should print out "label not found" and exit the program.

    But look at this:

    getlin(s)
    char s[];
    {
        int ch, i;
    
        i = 0;
    l:
        if ((ch=getc())=='\0') return(1);
        if (ch!=':') {
            while(ch!='\n' && ch!='\0')
                ch = getc();
            goto l;
            }
        while ((ch=getc())==' ');
        while (ch!=' ' && ch!='\n' && ch!='\0') {
            s[i++] = ch;
            ch = getc();
            }
        while(ch != '\n')
            ch = getc();
        s[i] = '\0';
        return(0);
    }
    

    getlin() will return 0(true) if we found the label and 1(false) if we didn't.

    But it should be the other way around or we would need to say:

    if (**!** getlin(line)){}
    

    What is wrong here?

    • arkascha
      arkascha over 9 years
      Not sure what this is about, but as a general rule of thumb: unix utilities traditionally return 0 (zero) to indicate success, everything else indicates an error (an error code). So I#d say the function implementation makes sense, the conditional is questionable.
    • Joey
      Joey over 9 years
      Yeah that's the question. But it's working, so why does it work?
  • FelixJN
    FelixJN about 8 years
    Would you need a '27p;q' to be faster or will sed quit automatically after hitting line 27?
  • 123
    123 about 8 years
    @Fiximan yes that will be quicker
  • heemayl
    heemayl about 8 years
    @Fiximan That surely would..edited..thanks..
  • Ravi Saini
    Ravi Saini about 8 years
    Thanx man this was much better for me.
  • don_crissti
    don_crissti over 5 years
    @Fiximan - sed would quit so quick that it won't print anything...
  • user1683793
    user1683793 over 2 years
    I set up a file with 500 million 100 byte records and for that case, this version works fastest for me at 1m2.582s followed by the sed version at 1m20.81s and awk at 2m14.756s
  • user1485864
    user1485864 over 2 years
    Plus this is more flexible. If the logs g-zipped you can simply add in the loop the zcat command.