0

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

  • separator can be text or number or special character – unknown Sep 2 '18 at 19:59
  • will the URL always have 4 fields? – msp9011 Sep 2 '18 at 20:03
  • url have always 4 fields – unknown Sep 2 '18 at 20:04
1

Try this,

awk -F '[/.]' '{print $0" (separator) |"$(NF-2)"|"$(NF-1)}' file

http://webpage.com/(name-never-changes)/aCcboeasdfdRD/asdfasdft21.jpg (saperatot) |aCcboeasdfdRD|asdfasdft21
  • /. two field separators
  • $0 to 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 '[/.]' '{print $0" (separator) |"$(NF-2)"|"$(NF-1)}' file`" > file

Option 2: (if it is a large file)

 awk -F '[/.]' '{print $0" (separator) |"$(NF-2)"|"$(NF-1)}' file > tempFile ; mv tempFile file

For multiple files in a folder:

cd /path/to/dir
for file in `ls`
do
    echo "`awk -F '[/.]' '{print $0" (separator) |"$(NF-2)"|"$(NF-1)}' $file`" > $file
done
  • use for loop. – msp9011 Sep 2 '18 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 '18 at 16:17
  • @unknown valid point!! we can validate by NF>2 – msp9011 Sep 4 '18 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 '18 at 16:33
  • @unknown yes, try the same code with NF>1 like awk -F '[/.]' 'NF>1{print.... – msp9011 Sep 4 '18 at 16:48
0

Here’s a sed solution:

sed 'h; s@^[^/]*//[^/]*/[^/]*/@  (separator)  |@; s@/@|@g; s/\.jpg$//; x; G; s/\n//'

Step by step:

  • h copies the current line from the “pattern space” to the “hold space”.
  • s@^[^/]*//[^/]*/[^/]*/@  (separator)  |@ is a substitute command using @ as the delimiter.  It replaces
    stuff//stuff/stuff/
    (i.e., http://example.com/(top-level-directory)/) with   (separator)  |.
    You could do s@^http//example.com/(top-level-directory)/@ (separator) |@ if you wanted to.
  • s@/@|@g is 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 the g.
  • s/\.jpg$// obviously removes the .jpg extension from the end of the line.  (If you don’t know what the extension is, you could do something like s/\.[^.]*$// instead.)
  • x exchanges 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.
  • G gets 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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.