New answers tagged

0 votes

Replace last word occurrence in file

Using any awk this will match the first space-separated string on a line without false matches even if that string contains regexp metachars or that string can appear as a substring of other strings: $...
Ed Morton's user avatar
  • 29.2k
0 votes

Replace last word occurrence in file

Yet, another awk solution: awk '{ lines[NR] = $0 /^Abc/ && (n = NR) } END { lines[n] = "#" lines[n] for (i = 1; i <= NR; i++) { ...
Raffa's user avatar
  • 214
0 votes

Replace last word occurrence in file

Using Raku (formerly known as Perl_6) ~$ raku -e 'my $i=0; lines.reverse.map( *.subst: /^Abc <?{$i++ == 0}> /, {"#$/"} ).reverse.join("\n").put;' file > tmp OR: ~$ raku -...
jubilatious1's user avatar
  • 2,463
0 votes

Replace last word occurrence in file

Using awk: Reversing the file, then awk's sub() function is used. After that reversing the file again gives expected output. $ tac file | awk '/^Abc/ && (!found){sub(/^Abc/, "#Abc"); ...
Prabhjot Singh's user avatar
2 votes

Replace last word occurrence in file

With awk (no pipe(s)): awk -v str=Abc ' NR==FNR{if ($0 == str) nr_str=NR; next} {print (FNR == nr_str) ? "#"$0 : $0} ' file file Output Abc 123 Abc Sdf 2 Abc #Abc Utyr Qww
Gilles Quénot's user avatar
2 votes

Replace last word occurrence in file

$ sed -e 'H; $!d' -e 'g; s/\n\(.*\n\)\(Abc\)/\1#\2/' file Abc 123 Abc Sdf 2 Abc #Abc Utyr Qww This reads the whole file into the hold space of sed, modifies it and outputs the modified text. The ...
Kusalananda's user avatar
  • 323k
1 vote

Replace last word occurrence in file

One approach is to process the file as a whole: perl -pi -0777 -pe 's/.*\K^Abc/#$&/ms' temp.sh Or without regexp and assuming the last occurrence of Abc at the start of the line is not on the ...
Stéphane Chazelas's user avatar
4 votes
Accepted

Replace last word occurrence in file

Reverse the file, comment out the first, and reverse the file back again: $ tac temp.sh | sed '0,/^Abc/{s/^Abc/#&/}' | tac Abc 123 Abc Sdf 2 Abc #Abc Utyr Qww That means "start at line 0, ...
terdon's user avatar
  • 235k
0 votes

Remove all lines after line contains?

Since the parent process (your shell) sets up redirection before your sed command runs, the > ~/.xinitrc clears the file before you can read it. Read man sed, and do something like UNTESTED: sed -i....
waltinator's user avatar
  • 4,483
0 votes
Accepted

How do I make sed delete a block of indented text below a non-indented line that also contains particular regexp, but not beyond?

Awk: $ awk '/deny/ { deny=1; next } /^ip/ { deny=0; print; next } !deny' < data ip prefix-list ispA_in_pref-150 ip prefix-list ispB_in_pref-100 ip prefix-list ispD_in_pref-50 seq ...
Kaz's user avatar
  • 7,781
0 votes

Cleanly swap all occurences of two strings using sed

I know this is from ages ago but in some cases you may use: echo foobar | sed -e 's/bar/foo/' -e 's/foo/bar/' This works because it replaces the first occurrence of bar first, and then the first ...
cherry-noize's user avatar
0 votes

sed "invalid command code W"

You could always just write whatever you want prepended in a here-document: #!/usr/bin/env bash tmp=$(mktemp) || exit trap 'rm -f -- "$tmp"; exit' EXIT shopt -s nullglob for file in *.vtt ;...
Ed Morton's user avatar
  • 29.2k
0 votes

sed "invalid command code W"

It would be easier and more portable with perl: perl -0777 -ni -e 'print qq(WEBVTT Kind: captions Language: eng File Creation Date: 2023-08 $_)' -- *.vtt sed scripts consist of a series of [<...
Stéphane Chazelas's user avatar
1 vote

Need to Replace multiple spaces between PIPE symbols with no space

The first match consumes the pipe at the end that needs to be part of the second match. I'd just loop-replace until no more matches are left instead of using the g modifier: sed -e ': loop' -e 's/| *|...
Simon Richter's user avatar
6 votes

Need to Replace multiple spaces between PIPE symbols with no space

With perl, you can replace all |<spaces> with | provided they're followed with a | using the (?=...) look-ahead regexp operator: perl -pe 's/\| +(?=\|)/|/g' your-file perl also has a -i option ...
Stéphane Chazelas's user avatar
7 votes

Need to Replace multiple spaces between PIPE symbols with no space

Using any sed: $ sed 's/| *|/||/g; s/| *|/||/g' file 1|123|A |Normal Behaviour Exhibit 2|345|B|Embedded|delimiter 3|678|D|dimension 1" 4|||| nvalue 5||||Missing cvalue or using any awk: $ awk '...
Ed Morton's user avatar
  • 29.2k
5 votes

Need to Replace multiple spaces between PIPE symbols with no space

Using awk: awk 'BEGIN{OFS=FS="|"}{for (i=1; i<=NF; i++) if ($i ~ /^ +$/) $i=""}1' file 1|123|A |Normal Behaviour Exhibit 2|345|B|Embedded|delimiter 3|678|D|dimension 1" ...
Gilles Quénot's user avatar
4 votes

Need to Replace multiple spaces between PIPE symbols with no space

Using Miller to read this as a pipe-delimited index-numbered (NIDX) file, cleaning up all whitespace and writing the result to standard output (use -I to do the change in the file "in-line"):...
Kusalananda's user avatar
  • 323k
0 votes

Delete a whole word from a CSV file that is not part of another word using SED

Using Raku (formerly known as Perl_6) ~$ raku -ne '.split(",").map(*.subst: :global, /^NULL$/ ).join(",").put;' file OR ~$ raku -ne '.split(",")>>.subst( :global, ...
jubilatious1's user avatar
  • 2,463
0 votes

awk on date conditions

Using Miller (mlr): $ mlr --csv filter 'strptime(${Date of birth}, "%d %b %Y") < "'$(date -d '-1 year' '+%s')'"' file Index,User Id,First Name,Last Name,Date of birth,Job Title ...
Prabhjot Singh's user avatar
-1 votes

awk on date conditions

IFS=$'\n' lyear_old_sec=$(date -d "1 year ago" +%s) echo '"Index","User Id","First Name","Last Name","Date of birth","Job Title"' ...
Praveen Kumar BS's user avatar
0 votes

Shell Script: Want to delete two consecutive lines matching pattern from specific line

Using ed: $ printf '%s\n' 'g/^ / s///\' '-,.j' 'g/^Name: /d' 'g/SHA-256-Digest: /d' '4,$g/^$/d' ,p Q | ed -s file Manifest-Version: 1.0 Version-Info: .... NetBeans-Simply-Convertible: {com/abc/xyz/...
Kusalananda's user avatar
  • 323k
1 vote

Delete a whole word from a CSV file that is not part of another word using SED

Using awk: awk '{sub(/^NULL,/, ","); gsub(/,NULL,/, ",,"); sub(/,NULL$/, ",")}1' file Using csvsql: file.csv as simple CSV file. 12345,George,MCNULLMAN,NULL,green,NULL ...
Prabhjot Singh's user avatar
2 votes

Delete a whole word from a CSV file that is not part of another word using SED

Using Miller (mlr) to empty each field that is the exact string NULL in the header-less CSV input file: $ cat file.csv 12345,George,MCNULLMAN,NULL,green,NULL $ mlr --csv -N put 'for (k,v in $*) { v ==...
Kusalananda's user avatar
  • 323k
2 votes
Accepted

Delete a whole word from a CSV file that is not part of another word using SED

When working with tabulated data, I would recommend using awk: awk 'BEGIN{FS=OFS=","}{for (i=1;i<=NF;i++) if ($i=="NULL") $i=""}1' input.csv This would set the input ...
AdminBee's user avatar
  • 21.8k
11 votes

How to modify a gzipped file with sed and then zip again the file?

zcat is really but a convenience function of gzip; to cite the gzip/gunzip/zcat manual page (man zcat): The zcat command is identical to gunzip -c. Just as you can use gunzip -c (or zcat) in a piped ...
Marcus Müller's user avatar
3 votes
Accepted

How to modify a gzipped file with sed and then zip again the file?

Simply add gzip in the pipe: zcat input.vcf.gz | sed 's/^chr//' | gzip > output.vcf.gz
treuss's user avatar
  • 283
0 votes

awk on date conditions

Since awk does not know about the quoting rules of CSV, it would be better to use a CSV-aware tool for performing this task. Using the CSV-processing tools from csvkit: $ csvsql -I --query "...
Kusalananda's user avatar
  • 323k
5 votes

awk on date conditions

Assumptions: all columns/fields are wrapped in double quotes double quotes do not show up as part of the data (otherwise we'll need something other than a basic -F'"' as a field separator) OP's (...
markp-fuso's user avatar
4 votes
Accepted

awk on date conditions

Using GNU awk and GNU date and 1 year condition: awk -v epoch1y=$(date -d '1 year ago' +%s) ' BEGIN{FPAT="([^,]*)|(\"[^\"]+\")"} NR>1{ cmd="date -d &...
Gilles Quénot's user avatar
0 votes

Remove duplicates by adding numerical suffix

Using Raku (formerly known as Perl_6) ~$ raku -ne 'BEGIN my %hash; put /^tag\:/ && %hash{$_}++ ?? $_ ~ sprintf("-%02d", %hash{$_}-1) !! $_;' file Above is the Raku version of an ...
jubilatious1's user avatar
  • 2,463
0 votes

Is there a way to prevent sed from interpreting the replacement string?

If you need to do this in a script, you can go with an escape function, i.e.: #!/bin/bash escvar () { sed -e 's/[\/&]/\\&/g' <<< $1 } replacement='https://google.com/?query=some\...
onestep.ua's user avatar
0 votes

how to rename multiple files by replacing string in file name? this string contains a "#"

If you are on windows using cygwin, then what works is: find . -type f -exec rename PhraseToReplace PlaceToReplaceWith {} \;
Antoni Papiewski's user avatar
2 votes

sed: update 2 similar variables in a file but keep the upper and lowercase

As a rule, you should not embed data in the code arguments of language interpreters whether they're shells, sed, awk, perl, python, etc. Doing so invariably introduces command injection ...
Stéphane Chazelas's user avatar
0 votes
Accepted

sed: update 2 similar variables in a file but keep the upper and lowercase

Use grouping \(...\) and referencing (\1 for the first group, \2 for the second etc.): sed -i "s/\(sid=\).*/\1${CName^^}/gI" dbca2.rsp For turning the user input to uppercase, the shell is ...
Philippos's user avatar
  • 13.3k
0 votes

How to remove newlines from an ip a s output?

If you prefer sed you could try this solution, which also has a nice explanation: You can use ^[[:digit:]] because of the output of ip. ip addr show | sed -e '/^[[:digit:]]/bpp' -e 'H;$bpp' -e 'd' -e '...
John Goofy's user avatar
0 votes

Use SED to replace part of a current variable with user input variable

To set the new name dynamically, by a variable, for instance (here only shown for the permanent change): Close to your attempt: sed -i "s/gdbName=*.MY.DOMAIN.COM/gdbName=$CDBName/" somefile....
user unknown's user avatar
  • 10.3k
0 votes
Accepted

Use SED to replace part of a current variable with user input variable

Assuming you want to modify a file wherein you have some line, gdbName=Test.MY.DOMAIN.COM Then this could be done using sed "s/^\(gdbName\)=Test\./\1=$CDBName./" somefile or, if you need ...
Kusalananda's user avatar
  • 323k
1 vote

Replace a block of spaces with a comma

Assuming you really only want to correct the header, you can replace all runs of space-like characters on the 1st line with commas: $ sed '1s/[[:space:]]\{1,\}/,/g' file A_DRIVERLICENSENUMBER_,...
Kusalananda's user avatar
  • 323k
3 votes
Accepted

Replace a block of spaces with a comma

Using any POSIX awk: $ awk -F' {3,}' -v OFS=',' '{$1=$1} 1' file A_DRIVERLICENSENUMBER_,A_PRIORADDRESS2_,A_MONTHLYRENT_,A_EMPLOYEEID_,A_WORKPHONESPECIALINSTR_,A_REFDETAIL_,A_VERBALPLEDGE, input ...
Ed Morton's user avatar
  • 29.2k
1 vote

How to use sed on symbolic linked files?

From the comment by the author of the question: Okay I used this way and it worked. for file in *_1.gz; do mv "$file" "${file%_1.fq.gz}_Ms_1.fq.gz"; done No need of any ...
minorChaos's user avatar
3 votes

Replace a block of spaces with a comma

Starting from your sample file, you can use Miller 6 and run mlr --ifs-regex " +" --csvlite --ragged cat input.txt to get A_DRIVERLICENSENUMBER_,A_PRIORADDRESS2_,A_MONTHLYRENT_,...
aborruso's user avatar
  • 2,708
3 votes

Replace a block of spaces with a comma

Using Raku (formerly known as Perl_6) ~$ raku -pe 's:g/ \s ** 3..* /,/;' file The Raku code above is similar to the Perl answer with as slight change in syntax, due to the fact that Raku enumerates ...
jubilatious1's user avatar
  • 2,463
5 votes

Replace a block of spaces with a comma

You can try: sed -E 's/[[:space:]]{3,}/,/g' file or perl -pe 's/\s{3,}/,/g' file
Gilles Quénot's user avatar
0 votes

Remove duplicates by adding numerical suffix

With perl: #!/usr/bin/perl use strict; use warnings; use feature qw/say/; my (%h, $c); while (<>) { chomp; if (/^tag:/) { $c = sprintf "%.2d", ++$h{$_}; if ($...
Gilles Quénot's user avatar
4 votes
Accepted

Remove duplicates by adding numerical suffix

awk can take arbitrary array indices - even a whole record ("line"). Make a regex match for tag: and start the counter, but correct by one due to the first match awk '$0 ~ /^tag:/ { n[$0]++?$...
FelixJN's user avatar
  • 12.9k
3 votes

Remove duplicates by adding numerical suffix

Here we go, exactly as required: awk ' NR==FNR{ if (/^tag:/) { a[$1]++ } next } { c=--a[$1] if (c>0) { printf "%s-%....
Gilles Quénot's user avatar
1 vote

awk add a column if it doesn't exist

For completeness and since the question is tagged sed as well, here is a not column-based but rather RegEx based sed solution (Notice: this is as good as its RegEx groups are ... So, tweak them as ...
Raffa's user avatar
  • 214
0 votes

How to extract logs between two time stamps

If anybody wants to get the logs within aparticular time range (maybe you want to get the logs within 5 min) you can do like shown below. It will fetch all the logs between the time range For dynamic ...
jayaprakash R's user avatar

Top 50 recent answers are included