Add Filename as first line of file in shell script
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Hello and thanks in advance.
I need to take a file, insert the filename as the first line of the file, then move to a different name. Here's the wrinkle. I need to grab the oldest file in the format of ORIGFILE_YYYYMMDD.TXT
and save it as NEWFILE.TXT
. For this example, let's say the file name is ORIGFILE_20151117.TXT
- Grab oldest file (
ls -tr ORIGFILE*.txt
) - Add
ORIGFILE_20151117.TXT
as first line of file - Rename/Move
ORIGFILE_20151117.TXT
toNEWFILE.TXT
shell-script text-processing files
bumped to the homepage by Community⦠6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |Â
up vote
2
down vote
favorite
Hello and thanks in advance.
I need to take a file, insert the filename as the first line of the file, then move to a different name. Here's the wrinkle. I need to grab the oldest file in the format of ORIGFILE_YYYYMMDD.TXT
and save it as NEWFILE.TXT
. For this example, let's say the file name is ORIGFILE_20151117.TXT
- Grab oldest file (
ls -tr ORIGFILE*.txt
) - Add
ORIGFILE_20151117.TXT
as first line of file - Rename/Move
ORIGFILE_20151117.TXT
toNEWFILE.TXT
shell-script text-processing files
bumped to the homepage by Community⦠6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Write the filename tonewfile.txt
and then append the entire contents oforigfile_20151117.txt
. After that delete the original file.
â roaima
Nov 17 '15 at 21:58
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Hello and thanks in advance.
I need to take a file, insert the filename as the first line of the file, then move to a different name. Here's the wrinkle. I need to grab the oldest file in the format of ORIGFILE_YYYYMMDD.TXT
and save it as NEWFILE.TXT
. For this example, let's say the file name is ORIGFILE_20151117.TXT
- Grab oldest file (
ls -tr ORIGFILE*.txt
) - Add
ORIGFILE_20151117.TXT
as first line of file - Rename/Move
ORIGFILE_20151117.TXT
toNEWFILE.TXT
shell-script text-processing files
Hello and thanks in advance.
I need to take a file, insert the filename as the first line of the file, then move to a different name. Here's the wrinkle. I need to grab the oldest file in the format of ORIGFILE_YYYYMMDD.TXT
and save it as NEWFILE.TXT
. For this example, let's say the file name is ORIGFILE_20151117.TXT
- Grab oldest file (
ls -tr ORIGFILE*.txt
) - Add
ORIGFILE_20151117.TXT
as first line of file - Rename/Move
ORIGFILE_20151117.TXT
toNEWFILE.TXT
shell-script text-processing files
shell-script text-processing files
edited Nov 17 '15 at 21:58
don_crissti
48.1k15126156
48.1k15126156
asked Nov 17 '15 at 21:46
user3191402
112
112
bumped to the homepage by Community⦠6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community⦠6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Write the filename tonewfile.txt
and then append the entire contents oforigfile_20151117.txt
. After that delete the original file.
â roaima
Nov 17 '15 at 21:58
add a comment |Â
Write the filename tonewfile.txt
and then append the entire contents oforigfile_20151117.txt
. After that delete the original file.
â roaima
Nov 17 '15 at 21:58
Write the filename to
newfile.txt
and then append the entire contents of origfile_20151117.txt
. After that delete the original file.â roaima
Nov 17 '15 at 21:58
Write the filename to
newfile.txt
and then append the entire contents of origfile_20151117.txt
. After that delete the original file.â roaima
Nov 17 '15 at 21:58
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
Well, let's break this down into simple steps:
#!/bin/bash
# First, let's get that file's name:
FILE=$(ls -rt ORIGFILE*.txt | tail -n1)
if [[ 0 -ne $? ]]; then
echo "Unable to locate matching file. Aborting." 1>&2
exit 1
fi
# Now, create a new file containing the file's name:
echo "$FILE" > NEWFILE.TXT
# And append the contents of the old file into the new:
cat "$FILE" >> NEWFILE.TXT
# Finally, get rid of the old file: (uncomment if you're sure)
# rm "$FILE"
add a comment |Â
up vote
0
down vote
This will do the trick:
f=$(ls -1tr ORIGFILE*.txt | head -1); echo $f | cat - $f > NEWFILE.txt && rm $f
This works also. Thanks for your help
â user3191402
Nov 17 '15 at 22:12
Only if the filenames don't contain nonprintable characters.
â Kusalananda
Oct 16 at 9:40
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
Well, let's break this down into simple steps:
#!/bin/bash
# First, let's get that file's name:
FILE=$(ls -rt ORIGFILE*.txt | tail -n1)
if [[ 0 -ne $? ]]; then
echo "Unable to locate matching file. Aborting." 1>&2
exit 1
fi
# Now, create a new file containing the file's name:
echo "$FILE" > NEWFILE.TXT
# And append the contents of the old file into the new:
cat "$FILE" >> NEWFILE.TXT
# Finally, get rid of the old file: (uncomment if you're sure)
# rm "$FILE"
add a comment |Â
up vote
0
down vote
Well, let's break this down into simple steps:
#!/bin/bash
# First, let's get that file's name:
FILE=$(ls -rt ORIGFILE*.txt | tail -n1)
if [[ 0 -ne $? ]]; then
echo "Unable to locate matching file. Aborting." 1>&2
exit 1
fi
# Now, create a new file containing the file's name:
echo "$FILE" > NEWFILE.TXT
# And append the contents of the old file into the new:
cat "$FILE" >> NEWFILE.TXT
# Finally, get rid of the old file: (uncomment if you're sure)
# rm "$FILE"
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Well, let's break this down into simple steps:
#!/bin/bash
# First, let's get that file's name:
FILE=$(ls -rt ORIGFILE*.txt | tail -n1)
if [[ 0 -ne $? ]]; then
echo "Unable to locate matching file. Aborting." 1>&2
exit 1
fi
# Now, create a new file containing the file's name:
echo "$FILE" > NEWFILE.TXT
# And append the contents of the old file into the new:
cat "$FILE" >> NEWFILE.TXT
# Finally, get rid of the old file: (uncomment if you're sure)
# rm "$FILE"
Well, let's break this down into simple steps:
#!/bin/bash
# First, let's get that file's name:
FILE=$(ls -rt ORIGFILE*.txt | tail -n1)
if [[ 0 -ne $? ]]; then
echo "Unable to locate matching file. Aborting." 1>&2
exit 1
fi
# Now, create a new file containing the file's name:
echo "$FILE" > NEWFILE.TXT
# And append the contents of the old file into the new:
cat "$FILE" >> NEWFILE.TXT
# Finally, get rid of the old file: (uncomment if you're sure)
# rm "$FILE"
answered Nov 17 '15 at 21:54
DopeGhoti
41.8k55180
41.8k55180
add a comment |Â
add a comment |Â
up vote
0
down vote
This will do the trick:
f=$(ls -1tr ORIGFILE*.txt | head -1); echo $f | cat - $f > NEWFILE.txt && rm $f
This works also. Thanks for your help
â user3191402
Nov 17 '15 at 22:12
Only if the filenames don't contain nonprintable characters.
â Kusalananda
Oct 16 at 9:40
add a comment |Â
up vote
0
down vote
This will do the trick:
f=$(ls -1tr ORIGFILE*.txt | head -1); echo $f | cat - $f > NEWFILE.txt && rm $f
This works also. Thanks for your help
â user3191402
Nov 17 '15 at 22:12
Only if the filenames don't contain nonprintable characters.
â Kusalananda
Oct 16 at 9:40
add a comment |Â
up vote
0
down vote
up vote
0
down vote
This will do the trick:
f=$(ls -1tr ORIGFILE*.txt | head -1); echo $f | cat - $f > NEWFILE.txt && rm $f
This will do the trick:
f=$(ls -1tr ORIGFILE*.txt | head -1); echo $f | cat - $f > NEWFILE.txt && rm $f
answered Nov 17 '15 at 22:02
Kira
2,932823
2,932823
This works also. Thanks for your help
â user3191402
Nov 17 '15 at 22:12
Only if the filenames don't contain nonprintable characters.
â Kusalananda
Oct 16 at 9:40
add a comment |Â
This works also. Thanks for your help
â user3191402
Nov 17 '15 at 22:12
Only if the filenames don't contain nonprintable characters.
â Kusalananda
Oct 16 at 9:40
This works also. Thanks for your help
â user3191402
Nov 17 '15 at 22:12
This works also. Thanks for your help
â user3191402
Nov 17 '15 at 22:12
Only if the filenames don't contain nonprintable characters.
â Kusalananda
Oct 16 at 9:40
Only if the filenames don't contain nonprintable characters.
â Kusalananda
Oct 16 at 9:40
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%2f243711%2fadd-filename-as-first-line-of-file-in-shell-script%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
Write the filename to
newfile.txt
and then append the entire contents oforigfile_20151117.txt
. After that delete the original file.â roaima
Nov 17 '15 at 21:58