Copy first line from one file to other files using xargs and sed
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a list of split CSV files. How can I copy the first CSV header to the rest of CSV files?
profiles00.csv
profiles01.csv
profiles02.csv
profiles03.csv
...
This is what I have now but how do I send the output to new files or replace existing files?
find . -name *.csv -print0 | xargs -0 -I -P 100 sed -e '1r ' -e 'q' profiles00.csv
shell-script sed xargs
add a comment |Â
up vote
0
down vote
favorite
I have a list of split CSV files. How can I copy the first CSV header to the rest of CSV files?
profiles00.csv
profiles01.csv
profiles02.csv
profiles03.csv
...
This is what I have now but how do I send the output to new files or replace existing files?
find . -name *.csv -print0 | xargs -0 -I -P 100 sed -e '1r ' -e 'q' profiles00.csv
shell-script sed xargs
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a list of split CSV files. How can I copy the first CSV header to the rest of CSV files?
profiles00.csv
profiles01.csv
profiles02.csv
profiles03.csv
...
This is what I have now but how do I send the output to new files or replace existing files?
find . -name *.csv -print0 | xargs -0 -I -P 100 sed -e '1r ' -e 'q' profiles00.csv
shell-script sed xargs
I have a list of split CSV files. How can I copy the first CSV header to the rest of CSV files?
profiles00.csv
profiles01.csv
profiles02.csv
profiles03.csv
...
This is what I have now but how do I send the output to new files or replace existing files?
find . -name *.csv -print0 | xargs -0 -I -P 100 sed -e '1r ' -e 'q' profiles00.csv
shell-script sed xargs
asked May 17 at 1:55
angelokh
1033
1033
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
Here's one way to handle your particular problem:
find . -maxdepth 1 -type f -name "profiles*.csv" ! -name "profiles00.csv" -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +
Written layed out in a multi-line manner as:
find . -maxdepth 1
-type f
-name "profiles*.csv" ! -name "profiles00.csv"
-exec sed -i -e '1!b'
-e 'R profiles00.csv'
-e 'N'
+
to be read as: find utility shall
-maxdepth 1
=> walk only in the current directory , normally it recurses down to all the way the lowest hierarchy.-type f
=> while walking select the regular files only.-name "profiles*.csv" ! -name "profiles00.csv"
=> out of all the regular files, select only thos who have profiles*.csv as their basenames AND filter out the "profiles00.csv" file since this is the header file.-exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +
-i
option => edit files in place GNU sed assumed-e
option => sed code follows1!b
=> don't touch the the non-first lines.R profiles00.csv
=> read one line (since this is the first time, it'll be the first line) from the profiles00.csv file. So essentially the header is fished out of the profiles00.csv file. But not printed yet.N
=> Read in the next line into the pattern space. this flushes out the read buffer and after that the actual pattern space is flushed out (lines 1,2) from the current file being operated upon by sed.
+
will make find supply the multiple files it has selected in one fell swoop to thesed
utility and GNU sed is capable of handling multiple files with the-i
option. Specifically, the line numbers are reset for a new file read in.
â ================ Posix way ======
LC_ALL=C
find . ! -name . -prune -type f
-name 'profiles??*.csv'
! -name 'profiles*[!0-9]*.csv'
! -name 'profiles00.csv'
-exec sh -c '
shift "$1"
head -n 1 < profiles00.csv > header
for arg do
printf "0r header\nw\nq\n" | ed -s "$arg"
done
' 2 1 +
Here we dispense with sed and take recourse to ed.
0r => will insert the contents of file mentioned, to the beginning of file being edited by ed.
w => will write to the file, thus freezing all the changes done.
q => will quit the ed editor.
1
This will handle arbitrary input (no need to sanitize the line to be inserted) but isgnu sed
specific. +1 nonetheless.
â don_crissti
May 17 at 12:16
add a comment |Â
up vote
1
down vote
You don't need xargs
Command :
find . -maxdepth 1 ! -name "profiles00.csv" -a -name "*csv" -exec sed -i "1s/^/$(head -1 profiles00.csv)n/" ;
Here I am using find
to select files and sed
for in place editing
Explanation
find
maxdepth 1
- to only select files in the immediate directory and not in sub directory, if you want to edit files in sub directory too, you can remove this option! -name "profiles00.csv" -a -name "*csv"
- to select all csv files but not profiles00.csv , here-a
means and and!
means not
sed
-i
- edit files in place1
- line addressings/^/$(head -1 profiles00.csv)n/
- to replace beginning of file with the given text
head
-1
- to select first line
add a comment |Â
up vote
0
down vote
This is an untested pseudo-code sketch so it may not work, but I'd go about it something like (first backup your files so you don't overwrite them):
sed -i '1s/^/'`head -n1 profiles00.csv`'/' *.csv
explained
For more information on inserting into files:
https://stackoverflow.com/questions/9533679/how-to-insert-a-text-at-the-beginning-of-a-file
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Here's one way to handle your particular problem:
find . -maxdepth 1 -type f -name "profiles*.csv" ! -name "profiles00.csv" -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +
Written layed out in a multi-line manner as:
find . -maxdepth 1
-type f
-name "profiles*.csv" ! -name "profiles00.csv"
-exec sed -i -e '1!b'
-e 'R profiles00.csv'
-e 'N'
+
to be read as: find utility shall
-maxdepth 1
=> walk only in the current directory , normally it recurses down to all the way the lowest hierarchy.-type f
=> while walking select the regular files only.-name "profiles*.csv" ! -name "profiles00.csv"
=> out of all the regular files, select only thos who have profiles*.csv as their basenames AND filter out the "profiles00.csv" file since this is the header file.-exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +
-i
option => edit files in place GNU sed assumed-e
option => sed code follows1!b
=> don't touch the the non-first lines.R profiles00.csv
=> read one line (since this is the first time, it'll be the first line) from the profiles00.csv file. So essentially the header is fished out of the profiles00.csv file. But not printed yet.N
=> Read in the next line into the pattern space. this flushes out the read buffer and after that the actual pattern space is flushed out (lines 1,2) from the current file being operated upon by sed.
+
will make find supply the multiple files it has selected in one fell swoop to thesed
utility and GNU sed is capable of handling multiple files with the-i
option. Specifically, the line numbers are reset for a new file read in.
â ================ Posix way ======
LC_ALL=C
find . ! -name . -prune -type f
-name 'profiles??*.csv'
! -name 'profiles*[!0-9]*.csv'
! -name 'profiles00.csv'
-exec sh -c '
shift "$1"
head -n 1 < profiles00.csv > header
for arg do
printf "0r header\nw\nq\n" | ed -s "$arg"
done
' 2 1 +
Here we dispense with sed and take recourse to ed.
0r => will insert the contents of file mentioned, to the beginning of file being edited by ed.
w => will write to the file, thus freezing all the changes done.
q => will quit the ed editor.
1
This will handle arbitrary input (no need to sanitize the line to be inserted) but isgnu sed
specific. +1 nonetheless.
â don_crissti
May 17 at 12:16
add a comment |Â
up vote
2
down vote
accepted
Here's one way to handle your particular problem:
find . -maxdepth 1 -type f -name "profiles*.csv" ! -name "profiles00.csv" -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +
Written layed out in a multi-line manner as:
find . -maxdepth 1
-type f
-name "profiles*.csv" ! -name "profiles00.csv"
-exec sed -i -e '1!b'
-e 'R profiles00.csv'
-e 'N'
+
to be read as: find utility shall
-maxdepth 1
=> walk only in the current directory , normally it recurses down to all the way the lowest hierarchy.-type f
=> while walking select the regular files only.-name "profiles*.csv" ! -name "profiles00.csv"
=> out of all the regular files, select only thos who have profiles*.csv as their basenames AND filter out the "profiles00.csv" file since this is the header file.-exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +
-i
option => edit files in place GNU sed assumed-e
option => sed code follows1!b
=> don't touch the the non-first lines.R profiles00.csv
=> read one line (since this is the first time, it'll be the first line) from the profiles00.csv file. So essentially the header is fished out of the profiles00.csv file. But not printed yet.N
=> Read in the next line into the pattern space. this flushes out the read buffer and after that the actual pattern space is flushed out (lines 1,2) from the current file being operated upon by sed.
+
will make find supply the multiple files it has selected in one fell swoop to thesed
utility and GNU sed is capable of handling multiple files with the-i
option. Specifically, the line numbers are reset for a new file read in.
â ================ Posix way ======
LC_ALL=C
find . ! -name . -prune -type f
-name 'profiles??*.csv'
! -name 'profiles*[!0-9]*.csv'
! -name 'profiles00.csv'
-exec sh -c '
shift "$1"
head -n 1 < profiles00.csv > header
for arg do
printf "0r header\nw\nq\n" | ed -s "$arg"
done
' 2 1 +
Here we dispense with sed and take recourse to ed.
0r => will insert the contents of file mentioned, to the beginning of file being edited by ed.
w => will write to the file, thus freezing all the changes done.
q => will quit the ed editor.
1
This will handle arbitrary input (no need to sanitize the line to be inserted) but isgnu sed
specific. +1 nonetheless.
â don_crissti
May 17 at 12:16
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Here's one way to handle your particular problem:
find . -maxdepth 1 -type f -name "profiles*.csv" ! -name "profiles00.csv" -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +
Written layed out in a multi-line manner as:
find . -maxdepth 1
-type f
-name "profiles*.csv" ! -name "profiles00.csv"
-exec sed -i -e '1!b'
-e 'R profiles00.csv'
-e 'N'
+
to be read as: find utility shall
-maxdepth 1
=> walk only in the current directory , normally it recurses down to all the way the lowest hierarchy.-type f
=> while walking select the regular files only.-name "profiles*.csv" ! -name "profiles00.csv"
=> out of all the regular files, select only thos who have profiles*.csv as their basenames AND filter out the "profiles00.csv" file since this is the header file.-exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +
-i
option => edit files in place GNU sed assumed-e
option => sed code follows1!b
=> don't touch the the non-first lines.R profiles00.csv
=> read one line (since this is the first time, it'll be the first line) from the profiles00.csv file. So essentially the header is fished out of the profiles00.csv file. But not printed yet.N
=> Read in the next line into the pattern space. this flushes out the read buffer and after that the actual pattern space is flushed out (lines 1,2) from the current file being operated upon by sed.
+
will make find supply the multiple files it has selected in one fell swoop to thesed
utility and GNU sed is capable of handling multiple files with the-i
option. Specifically, the line numbers are reset for a new file read in.
â ================ Posix way ======
LC_ALL=C
find . ! -name . -prune -type f
-name 'profiles??*.csv'
! -name 'profiles*[!0-9]*.csv'
! -name 'profiles00.csv'
-exec sh -c '
shift "$1"
head -n 1 < profiles00.csv > header
for arg do
printf "0r header\nw\nq\n" | ed -s "$arg"
done
' 2 1 +
Here we dispense with sed and take recourse to ed.
0r => will insert the contents of file mentioned, to the beginning of file being edited by ed.
w => will write to the file, thus freezing all the changes done.
q => will quit the ed editor.
Here's one way to handle your particular problem:
find . -maxdepth 1 -type f -name "profiles*.csv" ! -name "profiles00.csv" -exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +
Written layed out in a multi-line manner as:
find . -maxdepth 1
-type f
-name "profiles*.csv" ! -name "profiles00.csv"
-exec sed -i -e '1!b'
-e 'R profiles00.csv'
-e 'N'
+
to be read as: find utility shall
-maxdepth 1
=> walk only in the current directory , normally it recurses down to all the way the lowest hierarchy.-type f
=> while walking select the regular files only.-name "profiles*.csv" ! -name "profiles00.csv"
=> out of all the regular files, select only thos who have profiles*.csv as their basenames AND filter out the "profiles00.csv" file since this is the header file.-exec sed -i -e '1!b' -e 'R profiles00.csv' -e 'N' +
-i
option => edit files in place GNU sed assumed-e
option => sed code follows1!b
=> don't touch the the non-first lines.R profiles00.csv
=> read one line (since this is the first time, it'll be the first line) from the profiles00.csv file. So essentially the header is fished out of the profiles00.csv file. But not printed yet.N
=> Read in the next line into the pattern space. this flushes out the read buffer and after that the actual pattern space is flushed out (lines 1,2) from the current file being operated upon by sed.
+
will make find supply the multiple files it has selected in one fell swoop to thesed
utility and GNU sed is capable of handling multiple files with the-i
option. Specifically, the line numbers are reset for a new file read in.
â ================ Posix way ======
LC_ALL=C
find . ! -name . -prune -type f
-name 'profiles??*.csv'
! -name 'profiles*[!0-9]*.csv'
! -name 'profiles00.csv'
-exec sh -c '
shift "$1"
head -n 1 < profiles00.csv > header
for arg do
printf "0r header\nw\nq\n" | ed -s "$arg"
done
' 2 1 +
Here we dispense with sed and take recourse to ed.
0r => will insert the contents of file mentioned, to the beginning of file being edited by ed.
w => will write to the file, thus freezing all the changes done.
q => will quit the ed editor.
edited May 18 at 8:27
answered May 17 at 11:46
Rakesh Sharma
37813
37813
1
This will handle arbitrary input (no need to sanitize the line to be inserted) but isgnu sed
specific. +1 nonetheless.
â don_crissti
May 17 at 12:16
add a comment |Â
1
This will handle arbitrary input (no need to sanitize the line to be inserted) but isgnu sed
specific. +1 nonetheless.
â don_crissti
May 17 at 12:16
1
1
This will handle arbitrary input (no need to sanitize the line to be inserted) but is
gnu sed
specific. +1 nonetheless.â don_crissti
May 17 at 12:16
This will handle arbitrary input (no need to sanitize the line to be inserted) but is
gnu sed
specific. +1 nonetheless.â don_crissti
May 17 at 12:16
add a comment |Â
up vote
1
down vote
You don't need xargs
Command :
find . -maxdepth 1 ! -name "profiles00.csv" -a -name "*csv" -exec sed -i "1s/^/$(head -1 profiles00.csv)n/" ;
Here I am using find
to select files and sed
for in place editing
Explanation
find
maxdepth 1
- to only select files in the immediate directory and not in sub directory, if you want to edit files in sub directory too, you can remove this option! -name "profiles00.csv" -a -name "*csv"
- to select all csv files but not profiles00.csv , here-a
means and and!
means not
sed
-i
- edit files in place1
- line addressings/^/$(head -1 profiles00.csv)n/
- to replace beginning of file with the given text
head
-1
- to select first line
add a comment |Â
up vote
1
down vote
You don't need xargs
Command :
find . -maxdepth 1 ! -name "profiles00.csv" -a -name "*csv" -exec sed -i "1s/^/$(head -1 profiles00.csv)n/" ;
Here I am using find
to select files and sed
for in place editing
Explanation
find
maxdepth 1
- to only select files in the immediate directory and not in sub directory, if you want to edit files in sub directory too, you can remove this option! -name "profiles00.csv" -a -name "*csv"
- to select all csv files but not profiles00.csv , here-a
means and and!
means not
sed
-i
- edit files in place1
- line addressings/^/$(head -1 profiles00.csv)n/
- to replace beginning of file with the given text
head
-1
- to select first line
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You don't need xargs
Command :
find . -maxdepth 1 ! -name "profiles00.csv" -a -name "*csv" -exec sed -i "1s/^/$(head -1 profiles00.csv)n/" ;
Here I am using find
to select files and sed
for in place editing
Explanation
find
maxdepth 1
- to only select files in the immediate directory and not in sub directory, if you want to edit files in sub directory too, you can remove this option! -name "profiles00.csv" -a -name "*csv"
- to select all csv files but not profiles00.csv , here-a
means and and!
means not
sed
-i
- edit files in place1
- line addressings/^/$(head -1 profiles00.csv)n/
- to replace beginning of file with the given text
head
-1
- to select first line
You don't need xargs
Command :
find . -maxdepth 1 ! -name "profiles00.csv" -a -name "*csv" -exec sed -i "1s/^/$(head -1 profiles00.csv)n/" ;
Here I am using find
to select files and sed
for in place editing
Explanation
find
maxdepth 1
- to only select files in the immediate directory and not in sub directory, if you want to edit files in sub directory too, you can remove this option! -name "profiles00.csv" -a -name "*csv"
- to select all csv files but not profiles00.csv , here-a
means and and!
means not
sed
-i
- edit files in place1
- line addressings/^/$(head -1 profiles00.csv)n/
- to replace beginning of file with the given text
head
-1
- to select first line
answered May 17 at 5:32
mkmayank
36310
36310
add a comment |Â
add a comment |Â
up vote
0
down vote
This is an untested pseudo-code sketch so it may not work, but I'd go about it something like (first backup your files so you don't overwrite them):
sed -i '1s/^/'`head -n1 profiles00.csv`'/' *.csv
explained
For more information on inserting into files:
https://stackoverflow.com/questions/9533679/how-to-insert-a-text-at-the-beginning-of-a-file
add a comment |Â
up vote
0
down vote
This is an untested pseudo-code sketch so it may not work, but I'd go about it something like (first backup your files so you don't overwrite them):
sed -i '1s/^/'`head -n1 profiles00.csv`'/' *.csv
explained
For more information on inserting into files:
https://stackoverflow.com/questions/9533679/how-to-insert-a-text-at-the-beginning-of-a-file
add a comment |Â
up vote
0
down vote
up vote
0
down vote
This is an untested pseudo-code sketch so it may not work, but I'd go about it something like (first backup your files so you don't overwrite them):
sed -i '1s/^/'`head -n1 profiles00.csv`'/' *.csv
explained
For more information on inserting into files:
https://stackoverflow.com/questions/9533679/how-to-insert-a-text-at-the-beginning-of-a-file
This is an untested pseudo-code sketch so it may not work, but I'd go about it something like (first backup your files so you don't overwrite them):
sed -i '1s/^/'`head -n1 profiles00.csv`'/' *.csv
explained
For more information on inserting into files:
https://stackoverflow.com/questions/9533679/how-to-insert-a-text-at-the-beginning-of-a-file
answered May 17 at 3:11
Jonathan
7081612
7081612
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444274%2fcopy-first-line-from-one-file-to-other-files-using-xargs-and-sed%23new-answer', 'question_page');
);
Post as a guest
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
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
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