1

I have various text files I need to modify

test.xyz|test3.abc|test5232.lop|filename.test|file.text|qwerty.bat|... 

I'm trying to automate the process of removing "test5232.lop", including the proceeding the pipe, like this:

test.xyz|test3.abc|filename.test|file.text|qwerty.bat|... 

Without the need to generate a temp file if possible

1
  • 1
    If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you! Commented Nov 26, 2017 at 15:39

5 Answers 5

9

This looks like a job for cut. Tell it the delimiter is |, that we want to specify fields to drop, not fields to keep (--complement) and that the we want to select field 3 (to drop in this case).

Code:

 cut -d '|' --complement -f 3 

Test:

$ echo 'test.xyz|test3.abc|test5232.lop|filename.test|file.text|qwerty.bat|x' | cut -d '|' --complement -f 3 test.xyz|test3.abc|filename.test|file.text|qwerty.bat|x 
5
  • Nice! I mostly use POSIX features only, so I wasn't aware of the --complement option. Notably, this is GNU-specific. Commented Apr 1, 2017 at 1:04
  • I was looking at a more specific option, because its in text file and "test5232.lop" isn't always in the same place. Commented Apr 1, 2017 at 1:10
  • @JohnDoe6262, that's actually a much less interesting use case. Commented Apr 1, 2017 at 1:13
  • @JohnDoe6262, maybe @wildcard will modify his sed answer, because if just want to remove a specific string sed is super easy. Commented Apr 1, 2017 at 1:14
  • 1
    If your cut doesn't support --complement you can use a field list instead: cut -d '|' -f 1-2,4- Commented Apr 1, 2017 at 12:48
2

Just use Sed:

sed 's/|test5232\.lop//' file.txt 

Original answer, before request was clarified:

POSIX features only, using Sed:

sed 's/|[^|]*//2' file.txt 

If you know that all lines have at least three | symbols, you can use the more intuitive form:

sed 's/[^|]*|//3' file.txt 
2
  • This works well, can sed input/output to the same filename without needing to create a temp filename? Commented Apr 1, 2017 at 1:18
  • Not portably. Use -i option if your Sed supports it. (Check the man page for whether it requires an argument or not; that depends on what implementation you use.) Commented Apr 1, 2017 at 1:21
2

To replace the 3rd field (where a "field" is "anything except a pipe, zero or more times, followed by a pipe") with nothing:

awk '{$0=gensub(/[^|]*\|/, "", 3); print $0}' input 

Apparently you want to remove "test5232.lop" anywhere in the line:

sed -i 's/|test5232\.lop//' input 

(although any solution, including sed -i creates a temporary file)

1
  • gnu ed does not create a temporary file. gnu.org/software/ed/manual/ed_manual.html It can be obtuse to use, but when it comes to things like editing files in directories you can't execute, it's better to be obtuse and functional ^-^ Commented Feb 5, 2021 at 12:39
2
perl -F'[|]' -pale '$_ = join "|", grep $_ ne "test5232.lop", @F' yourfile perl -F'[|]' -pale '1 while s/(^|\|)\Ktest5232\.lop(\||$)//g' yourfile sed -e ' :clip s/[|]test5232\.lop[|]/|/ tclip s/^test5232\.lop[|]// s/[|]test5232\.lop$// s/^test5232\.lop$// ' yourfile 
2

Check also this awk simple solution. Will remove the string no matter where it is and should be portable:

$ a="test.xyz|test3.abc|test5232.lop|filename.test|file.text|qwerty.bat" $ awk -F"test5232.lop." '{printf("%s%s\n",$1,$2)}' <<<"$a" test.xyz|test3.abc|filename.test|file.text|qwerty.bat 

About your request for in-place editing , GNU AWK version > 4.1 also can make inplace edits according to gawk manual:

awk -i inplace -v INPLACE_SUFFIX=.bak '{...}' 

But in any case, neither awk nor sed nor perl can achieve a real inplace editing. GNU sed Info Pages clarify this issue for us:

'-i[SUFFIX]' '--in-place[=SUFFIX]' This option specifies that files are to be edited in-place. GNU 'sed' does this by creating a temporary file and sending output to this file rather than to the standard output.(1). 

Meaning that you can use any solution in-here by appending at the end something like this:

awk/sed/perl/whatever oldfile >tmpfile && mvtmpfile oldfile && rm -f tmpfile 
2
  • @JohnDoe6262 Clarification about in-place editing added Commented Apr 1, 2017 at 10:09
  • To be fair... ed, rather than sed can be used in-place, it's just... far more obtuse to use (must consult manual a lot), unless you actually take some time to run it interactively to figure out how the heck it works. gnu.org/software/ed/manual/ed_manual.html Commented Feb 5, 2021 at 12:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.