This question already has answers here:
regex to match a single character that is anything but a space
(3 answers)
Closed 2 years ago.
The git command:
git diff-tree --no-commit-id --name-status -r <commit hash>
generates a list that looks kinda like this:
D path/to/deleted/file.txt
A path/to/added/file.txt
A path/to/added/file.asd
M path/to/modified/file.txt
I want to grep out only the added and modified (A or M) txt files and their paths. I know I can do like this:
grep -v "^D"
to not include the deleted files.
and
grep -o "\w*.txt$"
to only get the txt files. But this command does not give me the path to the files. Since \w only matches the word. Is there any other wildcard that will match until the whitespace character (so that it removes the A/M with corresponding whitespace)?
Use \S to match anything that isn't whitespace.
grep -o '\S*\.txt$'
awk /^A/^M/'{$0=$NF} 1'
path/to/added/file.txt
path/to/added/file.asd
path/to/modified/file.txt
$
Related
This question already has answers here:
sed multiline delete with pattern
(2 answers)
Closed 2 years ago.
I need to remove strings from a text file that matches a REGEX pattern, using regex101 my pattern match works fine, but when I execute using sed, nothing gets deleted and for some reason the regex is not working:
https://regex101.com/r/oLNrDB/1/
I need to remove all occurrences of all text including newlines between the following 2 strings:
DELIMITER ;;
some text with newlines
DELIMITER ;
The sed command I am using is:
sed '/DELIMITER ;;[\S\s]*?DELIMITER ;/d' myfile.sql;
but the output is identical to the input file, what am I doing wrong ?
The problem here is that sed reads files line-by-line and applies the pattern to each line separately. In your case, this means that the one pattern can't match both the starting and finishing delimiter because no one line contains them both.
The sed way of doing this is to use a range with the delete command, /start pattern/,/end pattern/d, which means delete all lines between the start pattern and end pattern inclusive. For example
$ cat foo.txt
some text before
DELIMITER ;;
some text with newlines
DELIMITER ;
some text after
$ sed '/DELIMITER ;;/,/DELIMITER ;/d' foo.txt
some text before
some text after
This question already has answers here:
Is it possible to escape regex metacharacters reliably with sed
(4 answers)
Closed 2 years ago.
I want to replace \emph{G. fortis} with \emph{G. fortis}\index{\emph{Geospiza fortis}} to add terms to an index in a TeX document. I have a list of words in a file called gfortis that I will pass through the sed command in a while read -r command.
PREFTXt="\emph{G. fortis}" # text to search
REPLACETXT="$PREFTXt\index{\emph{Geospiza fortis}}" # text to replace
sed -e "s/${PREFTXt}/${REPLACETXT}/" path/chapt1.tex
The result is this:
\emph{G. fortis}index{emph{Geospiza fortis}}
But it should be:
\emph{G. fortis}\index{\emph{Geospiza fortis}}
The final command looks like that:
while read -r RP
do
echo "Adding $RP to the index"
PREFTXt="$RP"
ADDTXt="\index{\emph{Geospiza fortis}}"
REPLACETXT="$PREFTXt$ADDTXt"
echo "Replaced $RP with $REPLACETXT"
sed -e "s/${PREFTXt}/${REPLACETXT}/" path/chapt1.tex # should replace the text within this file.
done < path/words_index/gfortis # input the words file to replace with a certain \index command
The cap1.txt contains this:
\chapter{Another chapter in the wall}
NICE other\index{other} to be added to the index.
\emph{Geospiza fortis}
All of the stuff that I put here shall be into the index.
\emph{Geospiza fortis}
This index will be gigantic, but I won't be making multiple indexes.
\emph{G. fortis}
Other cool stuff here
\emph{G. fortis}
I'm using bash in macOS Mojave
You need to use a double backslash instead of a single one. This is because bash/shell etc. will interpret it as a special character and replace "\e" with "e".
To avoid having to escape those, you could put their contents in a file, for instance preftxt.cfg, and do something similar for the other file
it would contain
\emph{G. fortis}
And you could use it like this
PREFTXt="$(cat preftxt.cfg)"
use 3 backslash \ instead of one -- \\\
REPLACETXT="$PREFTXt\\\index{\\\emph{Geospiza fortis}}"
This question already has answers here:
Replace All Lines That Do Not Contain Matched String
(4 answers)
Closed 3 years ago.
I have a problem with making sed command, which gonna change lines, where =sometext= occurs and change it to another pattern, but will not do it when https occcurs in that line. I have no idea how I should change this command:sed -i 's/=\([^=]*\)=/{{\1}}/g'
You'll want to read the sed manual about matching lines: https://www.gnu.org/software/sed/manual/sed.html chapter 4:
The following command replaces the word ‘hello’ with ‘world’ only in lines not containing the word ‘apple’:
sed '/apple/!s/hello/world/' input.txt > output.txt
Use multiple blocks, e.g.:
sed '/=sometext=/ { /https/b; s/.../.../; }'
This question already has answers here:
Grep for literal strings
(6 answers)
Closed 4 years ago.
I want to copy over all lines in a file (file1.txt) containing Cmd:[41] over to another file (file2.txt)
awk '/Cmd:[41]/' file1.txt > file2.txt
This command doesn't seem to work. The file2.txt is of size 0. However there are lines in file1.txt that contains Cmd:[41]
Is there some specific awk escape character that I should be using. The problem is with [41] part. The other part of the search string seems to work fine.
You can just change your command in the following way and it will work:
awk '/Cmd:\[41\]/' file1.txt > file2.txt
Explanations:
'/Cmd:[41]/' will match lines that contain: Cmd:4 or Cmd:1 but will not match lines that contain literally Cmd:[41] as [...] are used in regex to define a character range, or a list of characters that can be matched therefore you need to escape them by adding a \ before them.
This question already has answers here:
Whether to escape ( and ) in regex using GNU sed
(4 answers)
Closed 4 years ago.
I need to insert '--' at the beginning of the line if line contains word VARCHAR(1000)
Sample of my file is:
TRIM(CAST("AP_RQ_MSG_TYPE_ID" AS NVARCHAR(1000))) AP_RQ_MSG_TYPE_ID,
TRIM(CAST("AP_RQ_PROCESSING_CD" AS NVARCHAR(1000)))
AP_RQ_PROCESSING_CD, TRIM(CAST("AP_RQ_ACQ_INST_ID" AS NVARCHAR(11)))
AP_RQ_ACQ_INST_ID, TRIM(CAST("AP_RQ_LOCAL_TXN_TIME" AS NVARCHAR(10)))
AP_RQ_LOCAL_TXN_TIME, TRIM(CAST("AP_RQ_LOCAL_TXN_DATE" AS
NVARCHAR(10))) AP_RQ_LOCAL_TXN_DATE, TRIM(CAST("AP_RQ_RETAILER" AS
NVARCHAR(11))) AP_RQ_RETAILER,
I used this command
sed 's/\(^.*VARCHAR\(1000\).*$\)/--\1/I' *.sql
But the result is not as expected.
Does anyone have idea what am I doing wrong?
this should do:
sed 's/.*VARCHAR(1000).*/--&/' file
The problem in your sed command is at the regex part. By default sed uses BRE, which means, the ( and ) (wrapping the 1000) are just literal brackets, you should not escape them, or you gave them special meaning: regex grouping.
The first and last (..) you have escaped, there you did right, if you want to reference it later by \1. so your problem is escape or not escape. :)
Use the following sed command:
sed '/VARCHAR(1000)/ s/.*/--\0/' *.sql
The s command appplies to all lines containing VARCHAR(1000). It then replaces the whole line .* by itself \0 with -- in front.
Through awk,
awk '/VARCHAR\(1000\)/ {sub (/^/,"--")}1' infile > outfile