Copy line in text file and add modified line to the end of line in same file

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I would like to copy line in text file edit it on the fly to replace all / to | and paste in on the same line in the same line.
I have the first line in the text file:
http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg
Copy text between (name-never-changes) and .jpg
/aCcboeasdfdRD/asdfasdft21
Change to / to |
|aCcboeasdfdRD|asdfasdft21
And add to the end of the same line where URL is, with a separator
the separator can be text or number or special character
http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (separator) |aCcboeasdfdRD|asdfasdft21
And this should work with multiple text files with different URLs
I can't create new files and merge them this should be on the fly because the name of text files are different and I don't know filenames
linux sed grep paste
add a comment |Â
up vote
0
down vote
favorite
I would like to copy line in text file edit it on the fly to replace all / to | and paste in on the same line in the same line.
I have the first line in the text file:
http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg
Copy text between (name-never-changes) and .jpg
/aCcboeasdfdRD/asdfasdft21
Change to / to |
|aCcboeasdfdRD|asdfasdft21
And add to the end of the same line where URL is, with a separator
the separator can be text or number or special character
http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (separator) |aCcboeasdfdRD|asdfasdft21
And this should work with multiple text files with different URLs
I can't create new files and merge them this should be on the fly because the name of text files are different and I don't know filenames
linux sed grep paste
separator can be text or number or special character
â unknown
Sep 2 at 19:59
will the URL always have 4 fields?
â msp9011
Sep 2 at 20:03
url have always 4 fields
â unknown
Sep 2 at 20:04
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I would like to copy line in text file edit it on the fly to replace all / to | and paste in on the same line in the same line.
I have the first line in the text file:
http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg
Copy text between (name-never-changes) and .jpg
/aCcboeasdfdRD/asdfasdft21
Change to / to |
|aCcboeasdfdRD|asdfasdft21
And add to the end of the same line where URL is, with a separator
the separator can be text or number or special character
http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (separator) |aCcboeasdfdRD|asdfasdft21
And this should work with multiple text files with different URLs
I can't create new files and merge them this should be on the fly because the name of text files are different and I don't know filenames
linux sed grep paste
I would like to copy line in text file edit it on the fly to replace all / to | and paste in on the same line in the same line.
I have the first line in the text file:
http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg
Copy text between (name-never-changes) and .jpg
/aCcboeasdfdRD/asdfasdft21
Change to / to |
|aCcboeasdfdRD|asdfasdft21
And add to the end of the same line where URL is, with a separator
the separator can be text or number or special character
http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (separator) |aCcboeasdfdRD|asdfasdft21
And this should work with multiple text files with different URLs
I can't create new files and merge them this should be on the fly because the name of text files are different and I don't know filenames
linux sed grep paste
linux sed grep paste
edited Sep 2 at 21:55
msp9011
3,46643862
3,46643862
asked Sep 2 at 19:54
unknown
305
305
separator can be text or number or special character
â unknown
Sep 2 at 19:59
will the URL always have 4 fields?
â msp9011
Sep 2 at 20:03
url have always 4 fields
â unknown
Sep 2 at 20:04
add a comment |Â
separator can be text or number or special character
â unknown
Sep 2 at 19:59
will the URL always have 4 fields?
â msp9011
Sep 2 at 20:03
url have always 4 fields
â unknown
Sep 2 at 20:04
separator can be text or number or special character
â unknown
Sep 2 at 19:59
separator can be text or number or special character
â unknown
Sep 2 at 19:59
will the URL always have 4 fields?
â msp9011
Sep 2 at 20:03
will the URL always have 4 fields?
â msp9011
Sep 2 at 20:03
url have always 4 fields
â unknown
Sep 2 at 20:04
url have always 4 fields
â unknown
Sep 2 at 20:04
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Try this,
awk -F '[/.]' '"$(NF-2)"' file
http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (saperatot) |aCcboeasdfdRD|asdfasdft21
/.two field separators$0to print the complete line$(NF-2) $(NF-1)the third and second fields from the end
if you want to save in the file
Option 1: (if it has few lines)
echo "`awk -F '[/.]' '"$(NF-2)"' file`" > file
Option 2: (if it is a large file)
awk -F '[/.]' '"$(NF-2)"' file > tempFile ; mv tempFile file
For multiple files in a folder:
cd /path/to/dir
for file in `ls`
do
echo "`awk -F '[/.]' '"$(NF-2)"' $file`" > $file
done
useforloop.
â msp9011
Sep 2 at 20:46
What happens if file contains empty line at end? This script dont gonna print in one line only print at second line, how i can deleted second empty line and print in one line
â unknown
Sep 4 at 16:17
@unknown valid point!! we can validate byNF>2
â msp9011
Sep 4 at 16:19
source is one line with text which i wanna cpy at the end of same line second line is empty thats why this script print at second line not on same line do You know script to deleted that second line before use script? Im a noob and i dont know what is vaild point
â unknown
Sep 4 at 16:33
@unknown yes, try the same code withNF>1likeawk -F '[/.]' 'NF>1{print....
â msp9011
Sep 4 at 16:48
 |Â
show 2 more comments
up vote
0
down vote
HereâÂÂs a sed solution:
sed 'h; s@^[^/]*//[^/]*/[^/]*/@ (separator) |@; s@/@|@g; s/.jpg$//; x; G; s/n//'
Step by step:
hcopies the current line from the âÂÂpattern spaceâ to the âÂÂhold spaceâÂÂ.s@^[^/]*//[^/]*/[^/]*/@àà(separator)àà|@is a substitute command
using@as the delimiter.ÃÂ
It replacesstuff//stuff/stuff/
(i.e.,http://example.com/(top-level-directory)/)
withÃÂ ÃÂ (separator)ÃÂ ÃÂ |.
You could dos@^http//example.com/(top-level-directory)/@ (separator) |@
if you wanted to.s@/@|@gis also a substitute command using@as the delimiter.ÃÂ
It replaces all remaining/characters in the line with|characters.ÃÂ
If youâÂÂre sure that there will be only one
(between(second-level-directory)and(filename)),
you can leave off theg.s/.jpg$//obviously removes the.jpgextension from the end of the line.ÃÂ
(If you donâÂÂt know what the extension is,
you could do something likes/.[^.]*$//instead.)xexchanges the pattern space and the hold space,
so the original line is now back in the pattern space, and theÃÂ ÃÂ (separator) |(second-level-directory)|(filename)
that we just built
is in the hold space.Ggets the contents of the hold space
and appends it to the pattern space (injecting a newline between them).s/n//removes that newline.
This will pass blank lines through as blank lines.ÃÂ
Other lines will be mangled.
If you want to edit file(s) in place, just pass sed the -i option
(or -i.bak, if desired or required on your system).
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Try this,
awk -F '[/.]' '"$(NF-2)"' file
http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (saperatot) |aCcboeasdfdRD|asdfasdft21
/.two field separators$0to print the complete line$(NF-2) $(NF-1)the third and second fields from the end
if you want to save in the file
Option 1: (if it has few lines)
echo "`awk -F '[/.]' '"$(NF-2)"' file`" > file
Option 2: (if it is a large file)
awk -F '[/.]' '"$(NF-2)"' file > tempFile ; mv tempFile file
For multiple files in a folder:
cd /path/to/dir
for file in `ls`
do
echo "`awk -F '[/.]' '"$(NF-2)"' $file`" > $file
done
useforloop.
â msp9011
Sep 2 at 20:46
What happens if file contains empty line at end? This script dont gonna print in one line only print at second line, how i can deleted second empty line and print in one line
â unknown
Sep 4 at 16:17
@unknown valid point!! we can validate byNF>2
â msp9011
Sep 4 at 16:19
source is one line with text which i wanna cpy at the end of same line second line is empty thats why this script print at second line not on same line do You know script to deleted that second line before use script? Im a noob and i dont know what is vaild point
â unknown
Sep 4 at 16:33
@unknown yes, try the same code withNF>1likeawk -F '[/.]' 'NF>1{print....
â msp9011
Sep 4 at 16:48
 |Â
show 2 more comments
up vote
1
down vote
accepted
Try this,
awk -F '[/.]' '"$(NF-2)"' file
http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (saperatot) |aCcboeasdfdRD|asdfasdft21
/.two field separators$0to print the complete line$(NF-2) $(NF-1)the third and second fields from the end
if you want to save in the file
Option 1: (if it has few lines)
echo "`awk -F '[/.]' '"$(NF-2)"' file`" > file
Option 2: (if it is a large file)
awk -F '[/.]' '"$(NF-2)"' file > tempFile ; mv tempFile file
For multiple files in a folder:
cd /path/to/dir
for file in `ls`
do
echo "`awk -F '[/.]' '"$(NF-2)"' $file`" > $file
done
useforloop.
â msp9011
Sep 2 at 20:46
What happens if file contains empty line at end? This script dont gonna print in one line only print at second line, how i can deleted second empty line and print in one line
â unknown
Sep 4 at 16:17
@unknown valid point!! we can validate byNF>2
â msp9011
Sep 4 at 16:19
source is one line with text which i wanna cpy at the end of same line second line is empty thats why this script print at second line not on same line do You know script to deleted that second line before use script? Im a noob and i dont know what is vaild point
â unknown
Sep 4 at 16:33
@unknown yes, try the same code withNF>1likeawk -F '[/.]' 'NF>1{print....
â msp9011
Sep 4 at 16:48
 |Â
show 2 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Try this,
awk -F '[/.]' '"$(NF-2)"' file
http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (saperatot) |aCcboeasdfdRD|asdfasdft21
/.two field separators$0to print the complete line$(NF-2) $(NF-1)the third and second fields from the end
if you want to save in the file
Option 1: (if it has few lines)
echo "`awk -F '[/.]' '"$(NF-2)"' file`" > file
Option 2: (if it is a large file)
awk -F '[/.]' '"$(NF-2)"' file > tempFile ; mv tempFile file
For multiple files in a folder:
cd /path/to/dir
for file in `ls`
do
echo "`awk -F '[/.]' '"$(NF-2)"' $file`" > $file
done
Try this,
awk -F '[/.]' '"$(NF-2)"' file
http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (saperatot) |aCcboeasdfdRD|asdfasdft21
/.two field separators$0to print the complete line$(NF-2) $(NF-1)the third and second fields from the end
if you want to save in the file
Option 1: (if it has few lines)
echo "`awk -F '[/.]' '"$(NF-2)"' file`" > file
Option 2: (if it is a large file)
awk -F '[/.]' '"$(NF-2)"' file > tempFile ; mv tempFile file
For multiple files in a folder:
cd /path/to/dir
for file in `ls`
do
echo "`awk -F '[/.]' '"$(NF-2)"' $file`" > $file
done
edited Sep 2 at 23:00
answered Sep 2 at 20:11
msp9011
3,46643862
3,46643862
useforloop.
â msp9011
Sep 2 at 20:46
What happens if file contains empty line at end? This script dont gonna print in one line only print at second line, how i can deleted second empty line and print in one line
â unknown
Sep 4 at 16:17
@unknown valid point!! we can validate byNF>2
â msp9011
Sep 4 at 16:19
source is one line with text which i wanna cpy at the end of same line second line is empty thats why this script print at second line not on same line do You know script to deleted that second line before use script? Im a noob and i dont know what is vaild point
â unknown
Sep 4 at 16:33
@unknown yes, try the same code withNF>1likeawk -F '[/.]' 'NF>1{print....
â msp9011
Sep 4 at 16:48
 |Â
show 2 more comments
useforloop.
â msp9011
Sep 2 at 20:46
What happens if file contains empty line at end? This script dont gonna print in one line only print at second line, how i can deleted second empty line and print in one line
â unknown
Sep 4 at 16:17
@unknown valid point!! we can validate byNF>2
â msp9011
Sep 4 at 16:19
source is one line with text which i wanna cpy at the end of same line second line is empty thats why this script print at second line not on same line do You know script to deleted that second line before use script? Im a noob and i dont know what is vaild point
â unknown
Sep 4 at 16:33
@unknown yes, try the same code withNF>1likeawk -F '[/.]' 'NF>1{print....
â msp9011
Sep 4 at 16:48
use
for loop.â msp9011
Sep 2 at 20:46
use
for loop.â msp9011
Sep 2 at 20:46
What happens if file contains empty line at end? This script dont gonna print in one line only print at second line, how i can deleted second empty line and print in one line
â unknown
Sep 4 at 16:17
What happens if file contains empty line at end? This script dont gonna print in one line only print at second line, how i can deleted second empty line and print in one line
â unknown
Sep 4 at 16:17
@unknown valid point!! we can validate by
NF>2â msp9011
Sep 4 at 16:19
@unknown valid point!! we can validate by
NF>2â msp9011
Sep 4 at 16:19
source is one line with text which i wanna cpy at the end of same line second line is empty thats why this script print at second line not on same line do You know script to deleted that second line before use script? Im a noob and i dont know what is vaild point
â unknown
Sep 4 at 16:33
source is one line with text which i wanna cpy at the end of same line second line is empty thats why this script print at second line not on same line do You know script to deleted that second line before use script? Im a noob and i dont know what is vaild point
â unknown
Sep 4 at 16:33
@unknown yes, try the same code with
NF>1 like awk -F '[/.]' 'NF>1{print....â msp9011
Sep 4 at 16:48
@unknown yes, try the same code with
NF>1 like awk -F '[/.]' 'NF>1{print....â msp9011
Sep 4 at 16:48
 |Â
show 2 more comments
up vote
0
down vote
HereâÂÂs a sed solution:
sed 'h; s@^[^/]*//[^/]*/[^/]*/@ (separator) |@; s@/@|@g; s/.jpg$//; x; G; s/n//'
Step by step:
hcopies the current line from the âÂÂpattern spaceâ to the âÂÂhold spaceâÂÂ.s@^[^/]*//[^/]*/[^/]*/@àà(separator)àà|@is a substitute command
using@as the delimiter.ÃÂ
It replacesstuff//stuff/stuff/
(i.e.,http://example.com/(top-level-directory)/)
withÃÂ ÃÂ (separator)ÃÂ ÃÂ |.
You could dos@^http//example.com/(top-level-directory)/@ (separator) |@
if you wanted to.s@/@|@gis also a substitute command using@as the delimiter.ÃÂ
It replaces all remaining/characters in the line with|characters.ÃÂ
If youâÂÂre sure that there will be only one
(between(second-level-directory)and(filename)),
you can leave off theg.s/.jpg$//obviously removes the.jpgextension from the end of the line.ÃÂ
(If you donâÂÂt know what the extension is,
you could do something likes/.[^.]*$//instead.)xexchanges the pattern space and the hold space,
so the original line is now back in the pattern space, and theÃÂ ÃÂ (separator) |(second-level-directory)|(filename)
that we just built
is in the hold space.Ggets the contents of the hold space
and appends it to the pattern space (injecting a newline between them).s/n//removes that newline.
This will pass blank lines through as blank lines.ÃÂ
Other lines will be mangled.
If you want to edit file(s) in place, just pass sed the -i option
(or -i.bak, if desired or required on your system).
add a comment |Â
up vote
0
down vote
HereâÂÂs a sed solution:
sed 'h; s@^[^/]*//[^/]*/[^/]*/@ (separator) |@; s@/@|@g; s/.jpg$//; x; G; s/n//'
Step by step:
hcopies the current line from the âÂÂpattern spaceâ to the âÂÂhold spaceâÂÂ.s@^[^/]*//[^/]*/[^/]*/@àà(separator)àà|@is a substitute command
using@as the delimiter.ÃÂ
It replacesstuff//stuff/stuff/
(i.e.,http://example.com/(top-level-directory)/)
withÃÂ ÃÂ (separator)ÃÂ ÃÂ |.
You could dos@^http//example.com/(top-level-directory)/@ (separator) |@
if you wanted to.s@/@|@gis also a substitute command using@as the delimiter.ÃÂ
It replaces all remaining/characters in the line with|characters.ÃÂ
If youâÂÂre sure that there will be only one
(between(second-level-directory)and(filename)),
you can leave off theg.s/.jpg$//obviously removes the.jpgextension from the end of the line.ÃÂ
(If you donâÂÂt know what the extension is,
you could do something likes/.[^.]*$//instead.)xexchanges the pattern space and the hold space,
so the original line is now back in the pattern space, and theÃÂ ÃÂ (separator) |(second-level-directory)|(filename)
that we just built
is in the hold space.Ggets the contents of the hold space
and appends it to the pattern space (injecting a newline between them).s/n//removes that newline.
This will pass blank lines through as blank lines.ÃÂ
Other lines will be mangled.
If you want to edit file(s) in place, just pass sed the -i option
(or -i.bak, if desired or required on your system).
add a comment |Â
up vote
0
down vote
up vote
0
down vote
HereâÂÂs a sed solution:
sed 'h; s@^[^/]*//[^/]*/[^/]*/@ (separator) |@; s@/@|@g; s/.jpg$//; x; G; s/n//'
Step by step:
hcopies the current line from the âÂÂpattern spaceâ to the âÂÂhold spaceâÂÂ.s@^[^/]*//[^/]*/[^/]*/@àà(separator)àà|@is a substitute command
using@as the delimiter.ÃÂ
It replacesstuff//stuff/stuff/
(i.e.,http://example.com/(top-level-directory)/)
withÃÂ ÃÂ (separator)ÃÂ ÃÂ |.
You could dos@^http//example.com/(top-level-directory)/@ (separator) |@
if you wanted to.s@/@|@gis also a substitute command using@as the delimiter.ÃÂ
It replaces all remaining/characters in the line with|characters.ÃÂ
If youâÂÂre sure that there will be only one
(between(second-level-directory)and(filename)),
you can leave off theg.s/.jpg$//obviously removes the.jpgextension from the end of the line.ÃÂ
(If you donâÂÂt know what the extension is,
you could do something likes/.[^.]*$//instead.)xexchanges the pattern space and the hold space,
so the original line is now back in the pattern space, and theÃÂ ÃÂ (separator) |(second-level-directory)|(filename)
that we just built
is in the hold space.Ggets the contents of the hold space
and appends it to the pattern space (injecting a newline between them).s/n//removes that newline.
This will pass blank lines through as blank lines.ÃÂ
Other lines will be mangled.
If you want to edit file(s) in place, just pass sed the -i option
(or -i.bak, if desired or required on your system).
HereâÂÂs a sed solution:
sed 'h; s@^[^/]*//[^/]*/[^/]*/@ (separator) |@; s@/@|@g; s/.jpg$//; x; G; s/n//'
Step by step:
hcopies the current line from the âÂÂpattern spaceâ to the âÂÂhold spaceâÂÂ.s@^[^/]*//[^/]*/[^/]*/@àà(separator)àà|@is a substitute command
using@as the delimiter.ÃÂ
It replacesstuff//stuff/stuff/
(i.e.,http://example.com/(top-level-directory)/)
withÃÂ ÃÂ (separator)ÃÂ ÃÂ |.
You could dos@^http//example.com/(top-level-directory)/@ (separator) |@
if you wanted to.s@/@|@gis also a substitute command using@as the delimiter.ÃÂ
It replaces all remaining/characters in the line with|characters.ÃÂ
If youâÂÂre sure that there will be only one
(between(second-level-directory)and(filename)),
you can leave off theg.s/.jpg$//obviously removes the.jpgextension from the end of the line.ÃÂ
(If you donâÂÂt know what the extension is,
you could do something likes/.[^.]*$//instead.)xexchanges the pattern space and the hold space,
so the original line is now back in the pattern space, and theÃÂ ÃÂ (separator) |(second-level-directory)|(filename)
that we just built
is in the hold space.Ggets the contents of the hold space
and appends it to the pattern space (injecting a newline between them).s/n//removes that newline.
This will pass blank lines through as blank lines.ÃÂ
Other lines will be mangled.
If you want to edit file(s) in place, just pass sed the -i option
(or -i.bak, if desired or required on your system).
answered Sep 8 at 0:40
G-Man
11.8k92658
11.8k92658
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%2f466442%2fcopy-line-in-text-file-and-add-modified-line-to-the-end-of-line-in-same-file%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
separator can be text or number or special character
â unknown
Sep 2 at 19:59
will the URL always have 4 fields?
â msp9011
Sep 2 at 20:03
url have always 4 fields
â unknown
Sep 2 at 20:04