Merge 2nd line of txt files togetherHow do I parse command line arguments in Bash?How do I tell if a regular file does not exist in Bash?How to count all the lines of code in a directory recursively?Linux: Merging multiple files, each on a new lineHow to merge two files line by line in BashHow to set a variable to the output of a command in Bash?Concatenate multiple files but include filename as section headersHow to merge every two lines into one from the command line?Read a file line by line assigning the value to a variableMerge two files using awk in linux
How much theory knowledge is actually used while playing?
How to convince somebody that he is fit for something else, but not this job?
Why do ¬, ∀ and ∃ have the same precedence?
Is there a nicer/politer/more positive alternative for "negates"?
"It doesn't matter" or "it won't matter"?
What kind of floor tile is this?
Giving feedback to someone without sounding prejudiced
Does the reader need to like the PoV character?
What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?
How do I tell my boss that I'm quitting soon, especially given that a colleague just left this week
Doesn't the system of the Supreme Court oppose justice?
PTIJ: Why is Haman obsessed with Bose?
Which was the first story featuring espers?
Do we have to expect a queue for the shuttle from Watford Junction to Harry Potter Studio?
The IT department bottlenecks progress, how should I handle this?
What fields between the rationals and the reals allow a good notion of 2D distance?
Change the color of a single dot in `ddot` symbol
How would you translate "more" for use as an interface button?
What features enable the Su-25 Frogfoot to operate with such a wide variety of fuels?
Is my low blitz game drawing rate at www.chess.com an indicator that I am weak in chess?
What's the name of the logical fallacy where a debater extends a statement far beyond the original statement to make it true?
Does the Linux kernel need a file system to run?
Why does this expression simplify as such?
What does Apple's new App Store requirement mean
Merge 2nd line of txt files together
How do I parse command line arguments in Bash?How do I tell if a regular file does not exist in Bash?How to count all the lines of code in a directory recursively?Linux: Merging multiple files, each on a new lineHow to merge two files line by line in BashHow to set a variable to the output of a command in Bash?Concatenate multiple files but include filename as section headersHow to merge every two lines into one from the command line?Read a file line by line assigning the value to a variableMerge two files using awk in linux
I have a few hundred txt files with 2 lines each.
To merge them, I would generally do:
cat *.txt > final.txt
however, I only need to do that for 2nd line of each file so the final output is like
2nd line of 1st file
2nd line of 2nd file
2nd line of 3rd file
(and so on..)
Any ideas how I can accomplish this?
bash awk cat
add a comment |
I have a few hundred txt files with 2 lines each.
To merge them, I would generally do:
cat *.txt > final.txt
however, I only need to do that for 2nd line of each file so the final output is like
2nd line of 1st file
2nd line of 2nd file
2nd line of 3rd file
(and so on..)
Any ideas how I can accomplish this?
bash awk cat
2
awk 'NR==2' file
will print the 2nd line of the file
– PS.
Mar 7 at 19:09
add a comment |
I have a few hundred txt files with 2 lines each.
To merge them, I would generally do:
cat *.txt > final.txt
however, I only need to do that for 2nd line of each file so the final output is like
2nd line of 1st file
2nd line of 2nd file
2nd line of 3rd file
(and so on..)
Any ideas how I can accomplish this?
bash awk cat
I have a few hundred txt files with 2 lines each.
To merge them, I would generally do:
cat *.txt > final.txt
however, I only need to do that for 2nd line of each file so the final output is like
2nd line of 1st file
2nd line of 2nd file
2nd line of 3rd file
(and so on..)
Any ideas how I can accomplish this?
bash awk cat
bash awk cat
edited Mar 7 at 23:47
RavinderSingh13
30.2k41639
30.2k41639
asked Mar 7 at 19:06
Allison64Allison64
82
82
2
awk 'NR==2' file
will print the 2nd line of the file
– PS.
Mar 7 at 19:09
add a comment |
2
awk 'NR==2' file
will print the 2nd line of the file
– PS.
Mar 7 at 19:09
2
2
awk 'NR==2' file
will print the 2nd line of the file– PS.
Mar 7 at 19:09
awk 'NR==2' file
will print the 2nd line of the file– PS.
Mar 7 at 19:09
add a comment |
5 Answers
5
active
oldest
votes
1st solution: Could you please try following with GNU awk
. nextfile
is very nice option in GNU awk
which will skip all lines in current Input_file when a condition is MET.
awk 'FNR==2print;nextfile' *.txt > output_file
2nd solution: In case you don't have GNU awk
try. Here since we are assuming there is NO nextfile
in awk
so I am creating a flag
on each file's 2nd line and when it is TRUE simply going next line/skipping them and trying to save sometime. NOTE that this flag value will be RESET on each file's first line too.
awk 'FNR==1flag="" FNR==2print;flag=1 flagnext' *.txt > output_file
3rd solution: Adding while
and find
approach too here with head
and tail
. AFAIK head and tail shouldn't read whole file.
while read line
do
head -n +2 "$line" | tail -1
done < <(find -type f -name "*.txt") > "output_file"
add a comment |
With GNU sed:
sed -n -s 2p *.txt > final.txt
or
sed -s '2!d' *.txt > final.txt
From man sed
:
-s
: consider files as separate rather than as a single continuous long stream.
1
-s
added to notes :)
– PS.
Mar 7 at 19:24
add a comment |
find . -name "*.txt" -type f -exec awk 'NR==2' ;
add a comment |
Use awk
awk 'FNR == 2 print; nextfile ' *.txt > final.txt
FNR
contains the line number within the current file. When it's on line 2, it will print the line and then go to the next file.
Hi Barmar sir, since you are an expert of bash would like to request you to please check my 3rd(while+find) answer if that will good in terms of speed etc, kindly provide your views on same.
– RavinderSingh13
Mar 7 at 19:29
1
It's acceptable, just very verbose and runs lots of processes.
– Barmar
Mar 7 at 19:31
Thank you for your view, appreciate it.
– RavinderSingh13
Mar 7 at 19:33
add a comment |
Using Perl
perl -ne ' if($.==2) print ; close(ARGV) ' *.txt
with sample files
$ cat allison1.txt
line1 in file1
line2 in file1
$ cat allison2.txt
line1 in file2
line2 in file2
$ cat allison3.txt
line1 in file3
line2 in file3
$ perl -ne ' if($.==2) print ; close(ARGV) ' allison*txt
line2 in file1
line2 in file2
line2 in file3
$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f55051127%2fmerge-2nd-line-of-txt-files-together%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
1st solution: Could you please try following with GNU awk
. nextfile
is very nice option in GNU awk
which will skip all lines in current Input_file when a condition is MET.
awk 'FNR==2print;nextfile' *.txt > output_file
2nd solution: In case you don't have GNU awk
try. Here since we are assuming there is NO nextfile
in awk
so I am creating a flag
on each file's 2nd line and when it is TRUE simply going next line/skipping them and trying to save sometime. NOTE that this flag value will be RESET on each file's first line too.
awk 'FNR==1flag="" FNR==2print;flag=1 flagnext' *.txt > output_file
3rd solution: Adding while
and find
approach too here with head
and tail
. AFAIK head and tail shouldn't read whole file.
while read line
do
head -n +2 "$line" | tail -1
done < <(find -type f -name "*.txt") > "output_file"
add a comment |
1st solution: Could you please try following with GNU awk
. nextfile
is very nice option in GNU awk
which will skip all lines in current Input_file when a condition is MET.
awk 'FNR==2print;nextfile' *.txt > output_file
2nd solution: In case you don't have GNU awk
try. Here since we are assuming there is NO nextfile
in awk
so I am creating a flag
on each file's 2nd line and when it is TRUE simply going next line/skipping them and trying to save sometime. NOTE that this flag value will be RESET on each file's first line too.
awk 'FNR==1flag="" FNR==2print;flag=1 flagnext' *.txt > output_file
3rd solution: Adding while
and find
approach too here with head
and tail
. AFAIK head and tail shouldn't read whole file.
while read line
do
head -n +2 "$line" | tail -1
done < <(find -type f -name "*.txt") > "output_file"
add a comment |
1st solution: Could you please try following with GNU awk
. nextfile
is very nice option in GNU awk
which will skip all lines in current Input_file when a condition is MET.
awk 'FNR==2print;nextfile' *.txt > output_file
2nd solution: In case you don't have GNU awk
try. Here since we are assuming there is NO nextfile
in awk
so I am creating a flag
on each file's 2nd line and when it is TRUE simply going next line/skipping them and trying to save sometime. NOTE that this flag value will be RESET on each file's first line too.
awk 'FNR==1flag="" FNR==2print;flag=1 flagnext' *.txt > output_file
3rd solution: Adding while
and find
approach too here with head
and tail
. AFAIK head and tail shouldn't read whole file.
while read line
do
head -n +2 "$line" | tail -1
done < <(find -type f -name "*.txt") > "output_file"
1st solution: Could you please try following with GNU awk
. nextfile
is very nice option in GNU awk
which will skip all lines in current Input_file when a condition is MET.
awk 'FNR==2print;nextfile' *.txt > output_file
2nd solution: In case you don't have GNU awk
try. Here since we are assuming there is NO nextfile
in awk
so I am creating a flag
on each file's 2nd line and when it is TRUE simply going next line/skipping them and trying to save sometime. NOTE that this flag value will be RESET on each file's first line too.
awk 'FNR==1flag="" FNR==2print;flag=1 flagnext' *.txt > output_file
3rd solution: Adding while
and find
approach too here with head
and tail
. AFAIK head and tail shouldn't read whole file.
while read line
do
head -n +2 "$line" | tail -1
done < <(find -type f -name "*.txt") > "output_file"
edited Mar 7 at 19:39
answered Mar 7 at 19:14
RavinderSingh13RavinderSingh13
30.2k41639
30.2k41639
add a comment |
add a comment |
With GNU sed:
sed -n -s 2p *.txt > final.txt
or
sed -s '2!d' *.txt > final.txt
From man sed
:
-s
: consider files as separate rather than as a single continuous long stream.
1
-s
added to notes :)
– PS.
Mar 7 at 19:24
add a comment |
With GNU sed:
sed -n -s 2p *.txt > final.txt
or
sed -s '2!d' *.txt > final.txt
From man sed
:
-s
: consider files as separate rather than as a single continuous long stream.
1
-s
added to notes :)
– PS.
Mar 7 at 19:24
add a comment |
With GNU sed:
sed -n -s 2p *.txt > final.txt
or
sed -s '2!d' *.txt > final.txt
From man sed
:
-s
: consider files as separate rather than as a single continuous long stream.
With GNU sed:
sed -n -s 2p *.txt > final.txt
or
sed -s '2!d' *.txt > final.txt
From man sed
:
-s
: consider files as separate rather than as a single continuous long stream.
edited Mar 7 at 19:25
answered Mar 7 at 19:23
CyrusCyrus
46.7k43880
46.7k43880
1
-s
added to notes :)
– PS.
Mar 7 at 19:24
add a comment |
1
-s
added to notes :)
– PS.
Mar 7 at 19:24
1
1
-s
added to notes :)– PS.
Mar 7 at 19:24
-s
added to notes :)– PS.
Mar 7 at 19:24
add a comment |
find . -name "*.txt" -type f -exec awk 'NR==2' ;
add a comment |
find . -name "*.txt" -type f -exec awk 'NR==2' ;
add a comment |
find . -name "*.txt" -type f -exec awk 'NR==2' ;
find . -name "*.txt" -type f -exec awk 'NR==2' ;
answered Mar 7 at 19:12
PS.PS.
7,577828
7,577828
add a comment |
add a comment |
Use awk
awk 'FNR == 2 print; nextfile ' *.txt > final.txt
FNR
contains the line number within the current file. When it's on line 2, it will print the line and then go to the next file.
Hi Barmar sir, since you are an expert of bash would like to request you to please check my 3rd(while+find) answer if that will good in terms of speed etc, kindly provide your views on same.
– RavinderSingh13
Mar 7 at 19:29
1
It's acceptable, just very verbose and runs lots of processes.
– Barmar
Mar 7 at 19:31
Thank you for your view, appreciate it.
– RavinderSingh13
Mar 7 at 19:33
add a comment |
Use awk
awk 'FNR == 2 print; nextfile ' *.txt > final.txt
FNR
contains the line number within the current file. When it's on line 2, it will print the line and then go to the next file.
Hi Barmar sir, since you are an expert of bash would like to request you to please check my 3rd(while+find) answer if that will good in terms of speed etc, kindly provide your views on same.
– RavinderSingh13
Mar 7 at 19:29
1
It's acceptable, just very verbose and runs lots of processes.
– Barmar
Mar 7 at 19:31
Thank you for your view, appreciate it.
– RavinderSingh13
Mar 7 at 19:33
add a comment |
Use awk
awk 'FNR == 2 print; nextfile ' *.txt > final.txt
FNR
contains the line number within the current file. When it's on line 2, it will print the line and then go to the next file.
Use awk
awk 'FNR == 2 print; nextfile ' *.txt > final.txt
FNR
contains the line number within the current file. When it's on line 2, it will print the line and then go to the next file.
answered Mar 7 at 19:12
BarmarBarmar
433k36257359
433k36257359
Hi Barmar sir, since you are an expert of bash would like to request you to please check my 3rd(while+find) answer if that will good in terms of speed etc, kindly provide your views on same.
– RavinderSingh13
Mar 7 at 19:29
1
It's acceptable, just very verbose and runs lots of processes.
– Barmar
Mar 7 at 19:31
Thank you for your view, appreciate it.
– RavinderSingh13
Mar 7 at 19:33
add a comment |
Hi Barmar sir, since you are an expert of bash would like to request you to please check my 3rd(while+find) answer if that will good in terms of speed etc, kindly provide your views on same.
– RavinderSingh13
Mar 7 at 19:29
1
It's acceptable, just very verbose and runs lots of processes.
– Barmar
Mar 7 at 19:31
Thank you for your view, appreciate it.
– RavinderSingh13
Mar 7 at 19:33
Hi Barmar sir, since you are an expert of bash would like to request you to please check my 3rd(while+find) answer if that will good in terms of speed etc, kindly provide your views on same.
– RavinderSingh13
Mar 7 at 19:29
Hi Barmar sir, since you are an expert of bash would like to request you to please check my 3rd(while+find) answer if that will good in terms of speed etc, kindly provide your views on same.
– RavinderSingh13
Mar 7 at 19:29
1
1
It's acceptable, just very verbose and runs lots of processes.
– Barmar
Mar 7 at 19:31
It's acceptable, just very verbose and runs lots of processes.
– Barmar
Mar 7 at 19:31
Thank you for your view, appreciate it.
– RavinderSingh13
Mar 7 at 19:33
Thank you for your view, appreciate it.
– RavinderSingh13
Mar 7 at 19:33
add a comment |
Using Perl
perl -ne ' if($.==2) print ; close(ARGV) ' *.txt
with sample files
$ cat allison1.txt
line1 in file1
line2 in file1
$ cat allison2.txt
line1 in file2
line2 in file2
$ cat allison3.txt
line1 in file3
line2 in file3
$ perl -ne ' if($.==2) print ; close(ARGV) ' allison*txt
line2 in file1
line2 in file2
line2 in file3
$
add a comment |
Using Perl
perl -ne ' if($.==2) print ; close(ARGV) ' *.txt
with sample files
$ cat allison1.txt
line1 in file1
line2 in file1
$ cat allison2.txt
line1 in file2
line2 in file2
$ cat allison3.txt
line1 in file3
line2 in file3
$ perl -ne ' if($.==2) print ; close(ARGV) ' allison*txt
line2 in file1
line2 in file2
line2 in file3
$
add a comment |
Using Perl
perl -ne ' if($.==2) print ; close(ARGV) ' *.txt
with sample files
$ cat allison1.txt
line1 in file1
line2 in file1
$ cat allison2.txt
line1 in file2
line2 in file2
$ cat allison3.txt
line1 in file3
line2 in file3
$ perl -ne ' if($.==2) print ; close(ARGV) ' allison*txt
line2 in file1
line2 in file2
line2 in file3
$
Using Perl
perl -ne ' if($.==2) print ; close(ARGV) ' *.txt
with sample files
$ cat allison1.txt
line1 in file1
line2 in file1
$ cat allison2.txt
line1 in file2
line2 in file2
$ cat allison3.txt
line1 in file3
line2 in file3
$ perl -ne ' if($.==2) print ; close(ARGV) ' allison*txt
line2 in file1
line2 in file2
line2 in file3
$
answered Mar 8 at 5:02
stack0114106stack0114106
4,7602423
4,7602423
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.
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%2f55051127%2fmerge-2nd-line-of-txt-files-together%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
2
awk 'NR==2' file
will print the 2nd line of the file– PS.
Mar 7 at 19:09