Get first 5 lines of a file, using a file of program names as input (Unix)
up vote
0
down vote
favorite
Goal: using an input file with a list of file names, get the first 5 lines of each file and output to another file. Basically, I'm trying to find out what each program does by reading the header.
Shell: Ksh
Input: myfile.txt
tmp/file1.txt
tmp/file2.txt
Output:
tmp/file1.txt - "Creates web login screen"
tmp/file2.txt - "Updates user login"
I can use "head -5" but not sure how to get the input from the file. I'm assuming I could redirect (>> output.txt)the output for my output file.
Input file names use a relative path.
Update: I created a script below but I'm getting "syntax error: unexpected end of file". The script was created with VI.
! /bin/sh
cat $HOME/jmarti20.list | while read line
do
#echo $line" >> jmarti20.txt
head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
done
shell unix
add a comment |
up vote
0
down vote
favorite
Goal: using an input file with a list of file names, get the first 5 lines of each file and output to another file. Basically, I'm trying to find out what each program does by reading the header.
Shell: Ksh
Input: myfile.txt
tmp/file1.txt
tmp/file2.txt
Output:
tmp/file1.txt - "Creates web login screen"
tmp/file2.txt - "Updates user login"
I can use "head -5" but not sure how to get the input from the file. I'm assuming I could redirect (>> output.txt)the output for my output file.
Input file names use a relative path.
Update: I created a script below but I'm getting "syntax error: unexpected end of file". The script was created with VI.
! /bin/sh
cat $HOME/jmarti20.list | while read line
do
#echo $line" >> jmarti20.txt
head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
done
shell unix
Did you have a question?
– mypetlion
Nov 6 at 17:09
"Creates web login screen" doesn't look like "5 lines", more like a single line.
– Benjamin W.
Nov 6 at 17:09
Also,/tmp/file1.txt
looks like an absolute path.
– Benjamin W.
Nov 6 at 17:11
How do I get input from a file as input to my "head -5" command? In the response from hellork, "file1.txt" is a file with the names of files. 5 lines is in general. Sometimes it will be 1 sometimes more than 5.
– Marty
Nov 6 at 18:03
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Goal: using an input file with a list of file names, get the first 5 lines of each file and output to another file. Basically, I'm trying to find out what each program does by reading the header.
Shell: Ksh
Input: myfile.txt
tmp/file1.txt
tmp/file2.txt
Output:
tmp/file1.txt - "Creates web login screen"
tmp/file2.txt - "Updates user login"
I can use "head -5" but not sure how to get the input from the file. I'm assuming I could redirect (>> output.txt)the output for my output file.
Input file names use a relative path.
Update: I created a script below but I'm getting "syntax error: unexpected end of file". The script was created with VI.
! /bin/sh
cat $HOME/jmarti20.list | while read line
do
#echo $line" >> jmarti20.txt
head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
done
shell unix
Goal: using an input file with a list of file names, get the first 5 lines of each file and output to another file. Basically, I'm trying to find out what each program does by reading the header.
Shell: Ksh
Input: myfile.txt
tmp/file1.txt
tmp/file2.txt
Output:
tmp/file1.txt - "Creates web login screen"
tmp/file2.txt - "Updates user login"
I can use "head -5" but not sure how to get the input from the file. I'm assuming I could redirect (>> output.txt)the output for my output file.
Input file names use a relative path.
Update: I created a script below but I'm getting "syntax error: unexpected end of file". The script was created with VI.
! /bin/sh
cat $HOME/jmarti20.list | while read line
do
#echo $line" >> jmarti20.txt
head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
done
shell unix
shell unix
edited Nov 7 at 17:25
asked Nov 6 at 16:59
Marty
11
11
Did you have a question?
– mypetlion
Nov 6 at 17:09
"Creates web login screen" doesn't look like "5 lines", more like a single line.
– Benjamin W.
Nov 6 at 17:09
Also,/tmp/file1.txt
looks like an absolute path.
– Benjamin W.
Nov 6 at 17:11
How do I get input from a file as input to my "head -5" command? In the response from hellork, "file1.txt" is a file with the names of files. 5 lines is in general. Sometimes it will be 1 sometimes more than 5.
– Marty
Nov 6 at 18:03
add a comment |
Did you have a question?
– mypetlion
Nov 6 at 17:09
"Creates web login screen" doesn't look like "5 lines", more like a single line.
– Benjamin W.
Nov 6 at 17:09
Also,/tmp/file1.txt
looks like an absolute path.
– Benjamin W.
Nov 6 at 17:11
How do I get input from a file as input to my "head -5" command? In the response from hellork, "file1.txt" is a file with the names of files. 5 lines is in general. Sometimes it will be 1 sometimes more than 5.
– Marty
Nov 6 at 18:03
Did you have a question?
– mypetlion
Nov 6 at 17:09
Did you have a question?
– mypetlion
Nov 6 at 17:09
"Creates web login screen" doesn't look like "5 lines", more like a single line.
– Benjamin W.
Nov 6 at 17:09
"Creates web login screen" doesn't look like "5 lines", more like a single line.
– Benjamin W.
Nov 6 at 17:09
Also,
/tmp/file1.txt
looks like an absolute path.– Benjamin W.
Nov 6 at 17:11
Also,
/tmp/file1.txt
looks like an absolute path.– Benjamin W.
Nov 6 at 17:11
How do I get input from a file as input to my "head -5" command? In the response from hellork, "file1.txt" is a file with the names of files. 5 lines is in general. Sometimes it will be 1 sometimes more than 5.
– Marty
Nov 6 at 18:03
How do I get input from a file as input to my "head -5" command? In the response from hellork, "file1.txt" is a file with the names of files. 5 lines is in general. Sometimes it will be 1 sometimes more than 5.
– Marty
Nov 6 at 18:03
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Right, you can append output with >>
to a file.
head -n 5 file1.txt >> file_descriptions.txt
You can also use sed
to print lines, from documentation at pinfo sed
.
sed 5q file1.txt >> file_descriptions.txt
Personal preference is to put file description in line 3, and only print line 3 of files.
sed -n 3p file1.txt >> file_descriptions.txt
The reasoning for using line 3 has to do with the first line often containing a "shebang" like #!/bin/bash, and the 2nd line having localization strings, such as # -*- coding: UTF-8 -*-
, to allow proper display of extra character glyphs and languages in terminals and text editors that support them.
Great response! How do I input a list of files instead of "file1.txt"? Meaning I have a file from another process that generated a bunch of file names.
– Marty
Nov 6 at 18:06
You could use awhile
loop andread
command to put each input line in a$filename
variable, and append that to the output file. mywiki.wooledge.org/BashFAQ/001
– hellork
Nov 11 at 6:02
add a comment |
up vote
0
down vote
Below is what I came up with and seems to work fairly well:
#! /bin/sh
cat $HOME/jmarti20.list | while read line
do
temp=$line
temp2=$(head -n 5 /los_prod/$line)
echo "$temp" "$temp2" >> jmarti20.txt
#echo "$line" >> jmarti20.txt
#head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
done
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Right, you can append output with >>
to a file.
head -n 5 file1.txt >> file_descriptions.txt
You can also use sed
to print lines, from documentation at pinfo sed
.
sed 5q file1.txt >> file_descriptions.txt
Personal preference is to put file description in line 3, and only print line 3 of files.
sed -n 3p file1.txt >> file_descriptions.txt
The reasoning for using line 3 has to do with the first line often containing a "shebang" like #!/bin/bash, and the 2nd line having localization strings, such as # -*- coding: UTF-8 -*-
, to allow proper display of extra character glyphs and languages in terminals and text editors that support them.
Great response! How do I input a list of files instead of "file1.txt"? Meaning I have a file from another process that generated a bunch of file names.
– Marty
Nov 6 at 18:06
You could use awhile
loop andread
command to put each input line in a$filename
variable, and append that to the output file. mywiki.wooledge.org/BashFAQ/001
– hellork
Nov 11 at 6:02
add a comment |
up vote
0
down vote
Right, you can append output with >>
to a file.
head -n 5 file1.txt >> file_descriptions.txt
You can also use sed
to print lines, from documentation at pinfo sed
.
sed 5q file1.txt >> file_descriptions.txt
Personal preference is to put file description in line 3, and only print line 3 of files.
sed -n 3p file1.txt >> file_descriptions.txt
The reasoning for using line 3 has to do with the first line often containing a "shebang" like #!/bin/bash, and the 2nd line having localization strings, such as # -*- coding: UTF-8 -*-
, to allow proper display of extra character glyphs and languages in terminals and text editors that support them.
Great response! How do I input a list of files instead of "file1.txt"? Meaning I have a file from another process that generated a bunch of file names.
– Marty
Nov 6 at 18:06
You could use awhile
loop andread
command to put each input line in a$filename
variable, and append that to the output file. mywiki.wooledge.org/BashFAQ/001
– hellork
Nov 11 at 6:02
add a comment |
up vote
0
down vote
up vote
0
down vote
Right, you can append output with >>
to a file.
head -n 5 file1.txt >> file_descriptions.txt
You can also use sed
to print lines, from documentation at pinfo sed
.
sed 5q file1.txt >> file_descriptions.txt
Personal preference is to put file description in line 3, and only print line 3 of files.
sed -n 3p file1.txt >> file_descriptions.txt
The reasoning for using line 3 has to do with the first line often containing a "shebang" like #!/bin/bash, and the 2nd line having localization strings, such as # -*- coding: UTF-8 -*-
, to allow proper display of extra character glyphs and languages in terminals and text editors that support them.
Right, you can append output with >>
to a file.
head -n 5 file1.txt >> file_descriptions.txt
You can also use sed
to print lines, from documentation at pinfo sed
.
sed 5q file1.txt >> file_descriptions.txt
Personal preference is to put file description in line 3, and only print line 3 of files.
sed -n 3p file1.txt >> file_descriptions.txt
The reasoning for using line 3 has to do with the first line often containing a "shebang" like #!/bin/bash, and the 2nd line having localization strings, such as # -*- coding: UTF-8 -*-
, to allow proper display of extra character glyphs and languages in terminals and text editors that support them.
edited Nov 11 at 6:06
answered Nov 6 at 17:21
hellork
1035
1035
Great response! How do I input a list of files instead of "file1.txt"? Meaning I have a file from another process that generated a bunch of file names.
– Marty
Nov 6 at 18:06
You could use awhile
loop andread
command to put each input line in a$filename
variable, and append that to the output file. mywiki.wooledge.org/BashFAQ/001
– hellork
Nov 11 at 6:02
add a comment |
Great response! How do I input a list of files instead of "file1.txt"? Meaning I have a file from another process that generated a bunch of file names.
– Marty
Nov 6 at 18:06
You could use awhile
loop andread
command to put each input line in a$filename
variable, and append that to the output file. mywiki.wooledge.org/BashFAQ/001
– hellork
Nov 11 at 6:02
Great response! How do I input a list of files instead of "file1.txt"? Meaning I have a file from another process that generated a bunch of file names.
– Marty
Nov 6 at 18:06
Great response! How do I input a list of files instead of "file1.txt"? Meaning I have a file from another process that generated a bunch of file names.
– Marty
Nov 6 at 18:06
You could use a
while
loop and read
command to put each input line in a $filename
variable, and append that to the output file. mywiki.wooledge.org/BashFAQ/001– hellork
Nov 11 at 6:02
You could use a
while
loop and read
command to put each input line in a $filename
variable, and append that to the output file. mywiki.wooledge.org/BashFAQ/001– hellork
Nov 11 at 6:02
add a comment |
up vote
0
down vote
Below is what I came up with and seems to work fairly well:
#! /bin/sh
cat $HOME/jmarti20.list | while read line
do
temp=$line
temp2=$(head -n 5 /los_prod/$line)
echo "$temp" "$temp2" >> jmarti20.txt
#echo "$line" >> jmarti20.txt
#head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
done
add a comment |
up vote
0
down vote
Below is what I came up with and seems to work fairly well:
#! /bin/sh
cat $HOME/jmarti20.list | while read line
do
temp=$line
temp2=$(head -n 5 /los_prod/$line)
echo "$temp" "$temp2" >> jmarti20.txt
#echo "$line" >> jmarti20.txt
#head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
done
add a comment |
up vote
0
down vote
up vote
0
down vote
Below is what I came up with and seems to work fairly well:
#! /bin/sh
cat $HOME/jmarti20.list | while read line
do
temp=$line
temp2=$(head -n 5 /los_prod/$line)
echo "$temp" "$temp2" >> jmarti20.txt
#echo "$line" >> jmarti20.txt
#head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
done
Below is what I came up with and seems to work fairly well:
#! /bin/sh
cat $HOME/jmarti20.list | while read line
do
temp=$line
temp2=$(head -n 5 /los_prod/$line)
echo "$temp" "$temp2" >> jmarti20.txt
#echo "$line" >> jmarti20.txt
#head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
done
answered Nov 15 at 12:50
Marty
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53176547%2fget-first-5-lines-of-a-file-using-a-file-of-program-names-as-input-unix%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Did you have a question?
– mypetlion
Nov 6 at 17:09
"Creates web login screen" doesn't look like "5 lines", more like a single line.
– Benjamin W.
Nov 6 at 17:09
Also,
/tmp/file1.txt
looks like an absolute path.– Benjamin W.
Nov 6 at 17:11
How do I get input from a file as input to my "head -5" command? In the response from hellork, "file1.txt" is a file with the names of files. 5 lines is in general. Sometimes it will be 1 sometimes more than 5.
– Marty
Nov 6 at 18:03