0

i need to fetch the nth line of a txt file using shell script.

my text file is like

abc
xyz

i need to fetch the 2nd line and store it in a variable

i've tried all combinations using commands like :

  1. sed
  2. awk
  3. head
  4. tail
  5. cat

... etc

problem is, when the script is called from the terminal, all these commands work fine. but when i call the same shell script, from my java file, these commands do not work.

I expect, it has something to do with the non-interactive shell.

Please help

PS : using read command i'm able to store the first line in a variable.

read -r i<edit.txt

here , "i" is the variable and edit.txt is my txt file.

but i cant figure out, how to get the second line.

thanks in advance

edit : ALso the script exits, when i use these "non-working" commands, And none of the remaining commands is executed.

already tried commands :

i=`awk 'N==2' edit.txt`
i=$(tail -n 1 edit.txt)
i=$(cat edit.txt | awk 'N==2')
i=$(grep "x" edit.txt)

java code:

try
    {
        ProcessBuilder pb = new ProcessBuilder("./myScript.sh",someParam);

        pb.environment().put("PATH", "OtherPath");

        Process p = pb.start(); 

        InputStreamReader isr = new InputStreamReader(p.getInputStream());
        BufferedReader br = new BufferedReader(isr);

        String line ;
        while((line = br.readLine()) != null)
           System.out.println(line);

        int exitVal = p.waitFor();
    }catch(Exception e)
    {  e.printStackTrace();  }
}

myscript.sh

read -r i<edit.txt
echo "session is : "$i    #this prints abc, as required.

resFile=$(echo `sed -n '2p' edit.txt`)    #this ans other similar commands donot do anything. 
echo "file path is : "$resFile
Singh
  • 63
  • 1
  • 2
  • 10
  • Possible dup: http://stackoverflow.com/questions/6022384/bash-tool-to-get-nth-line-from-a-file – UltraInstinct Dec 19 '13 at 07:01
  • i've tried his command, and it works well in the terminal. But not when the script is called from the the java program. – Singh Dec 19 '13 at 07:03

2 Answers2

3

An efficient way to print nth line from a file (especially suited for large files):

sed '2q;d' file

This sed command quits just after printing 2nd line rather than reading file till the end.

To store this in a variable:

line=$(sed '2q;d' file)

OR using a variable for line #:

n=2
line=$(sed $n'q;d' file)

UPDATE:

Java Code:

try {
    ProcessBuilder pb = new ProcessBuilder("/bin/bash", "/full/path/of/myScript.sh" );
    Process pr = pb.start(); 
    InputStreamReader isr = new InputStreamReader(pr.getInputStream());
    BufferedReader br = new BufferedReader(isr);
    String line;
    while((line = br.readLine()) != null)
        System.out.println(line);
    int exitVal = pr.waitFor();
    System.out.println("exitVal: " + exitVal);
} catch(Exception e) {  e.printStackTrace();  }

Shell script:

f=$(dirname $0)/edit.txt
read -r i < "$f"
echo "session is: $i"

echo -n "file path is: "
sed '2q;d' "$f"
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • works in the terminal, but not when same script is called by the java program – Singh Dec 19 '13 at 07:30
  • added the myscript.sh – Singh Dec 19 '13 at 09:50
  • I updated my answer with my suggested shell script. Can you try it now. – anubhava Dec 19 '13 at 10:04
  • See update once again with both Java and shell script code. This is fully tested code btw – anubhava Dec 19 '13 at 10:26
  • sir, the edit.txt file is lying in the current directory. Path cant be hard coded. – Singh Dec 19 '13 at 10:49
  • hi anubhava this is also not working. may be the problem lies with the non-interactive shell. when u run the java code, you would have run it with java command. But my program runs from a jar file. Which makes all the commands run in back ground. That's why these command dont seem to work. – Singh Dec 19 '13 at 13:44
  • No running it from JAR doesn't force any background processing unless you have some code not shown in the question. I believe my answer has already gone well beyond the original question which was merely to get nth line from a file in a shell script. I would suggest you accept this answer and create a new one with your specific problem. – anubhava Dec 19 '13 at 13:47
  • ok, i'm puttin a new question. thanks anyways.. i've been trying to figure it out for a long tym, and still could find anything. – Singh Dec 19 '13 at 13:51
  • i've added a new ques : http://stackoverflow.com/questions/20683873/how-to-launch-a-shell-script-in-a-new-gnome-terminal-from-a-java-program – Singh Dec 19 '13 at 14:14
  • Thanks let me take a look at it. – anubhava Dec 19 '13 at 14:17
0

Try this:

tail -n+X file.txt | head -1

where X is your line number:

tail -n+4 file.txt | head -1

for the 4th line.

michali
  • 401
  • 2
  • 11
  • Also a shorter version is possible: tail -2 file.txt | head -1 And of course you grab the output and store in a variable: line=\`tail -2 file.txt | head -1\` – Bartosz Klimek Dec 19 '13 at 07:28
  • works in the terminal, but not when same script is called by the java program – Singh Dec 19 '13 at 07:30