0

I have file called log.txt. file contains are like below :-

/proc
used    avail
10      100

how can i extract the below strings from that file using shell script. I want the below strings to be extracted.

/proc
10
100
3
  • 2
    Don't forget to accept the most useful answers on your existing questions. Commented Aug 8, 2011 at 5:37
  • Following your comments on Eric's answer and my own, could you please edit your question to include the actual contents of log.txt as well as your actual expected output rather than expecting the StackOverflow community to guess. Thanks ever-so! Commented Aug 8, 2011 at 6:43
  • Aha. Thanks @dogbane. A little formatting makes the World of difference. Commented Aug 8, 2011 at 9:59

4 Answers 4

1
awk '{print $1 $4 $5}' log.txt
Sign up to request clarification or add additional context in comments.

2 Comments

thx, but actually the file content is not like above. its like :- In the first line its only /proc. in the second line its "used" and "avail" and in the next line "10" and "100". below "used" its "10" and below "avail" its "100". the structure is like this. hope u can uderstand what i want to describe here
please have a look at my question in 1st page(scroll up)... ur answer is not working for that
1
awk '/\/proc/ {print;getline;getline;print $1"\n"$2}' log.txt

The above awk command calls getline twice whenever a line matches /proc. The print statement then prints out the second line after the match.

Output:

/proc
10
100

Comments

1

Using sed, and if it is spaces you have between 10 and 100:

sed -e '2d;3s/  */\n/' log.txt

if it is tabs you have between 10 and 100, and you gave GNU sed:

sed -e '2d;3s/\t\t*/\n/' log.txt

if it is tabs you have between 10 and 100, and you do not have gave GNU sed, but real tabs instead of the 2 \t above.

Comments

0

Avoiding sed (or awk) and using standard UNIX utilities, and if it is spaces you have between 10 and 100:

paste -s -d " " file | tr -s " " | cut -d " " -f 1,4,5 | tr " " "\n"

if it is tabs you have between 10 and 100, and you have GNU utilities:

paste -s -d "\t" file | tr -s "\t" | cut -f 1,4,5 | tr "\t" "\n"

if it is tabs you have between 10 and 100, and you do not have gave GNU utilities, but real tabs instead of the \t above and a real instead of the \n.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.