RegEx - How to extract second matches using grep [duplicate] - regex

This question already has answers here:
Parsing the first column of a csv file to a new file
(4 answers)
Closed 3 years ago.
How to extract second numbers in this text using grep command in ubuntu.
14219,98.2,31-s,19225
39219,92.7,31-s,29225
39219,38.7,31-s,29225
ouput desired :
98.2
92.7
38.7
Thanks

Possibly a duplicate of this post: Parsing the first column of a csv file to a new file
In your case, you can use cat file.txt | cut -d, -f2 to extract the second column.

Related

How to use grep to filter out lines containing a specific number [duplicate]

This question already has answers here:
Display exact matches only with grep [duplicate]
(9 answers)
grep,how to search for exact pattern?
(3 answers)
Closed 1 year ago.
I want to use grep to filter a txt file. I just want lines containing number 35. I use grep command:
grep -iE "35" File.txt
and I get a result txt which contains many strings like:
9.335837
nal.cpp:3517
These strings contain "35", but that's not what I want. I just want number "35". But strings like:
fd=35
FD:35
ID 35,
are OK too. What grep command will help me do that?

Matching negative and positive pattern in one sed [duplicate]

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/.../.../; }'

Copy lines containing a specific text from one file to another file [duplicate]

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.

Use grep to get next word after match [duplicate]

This question already has answers here:
Using grep to get the next WORD after a match in each line
(6 answers)
Closed 9 months ago.
I want to use grep to get a number from a JSON file
For example, I want to get the 1.0872 from this:
{"base":"EUR","date":"2016-03-01","rates":{"USD":1.0872}}
Using
grep "USD" rates
gives out the whole line
{"base":"EUR","date":"2016-03-01","rates":{"USD":1.0872}}
I just want to display 1.0872.
I tried using a regex but it doesn't work (probably an error on my part since I've never done this before):
grep -oP '(?<="USD"\:)\w+' file
For "normal" integers and float values, you may use
grep -oP '(?<="USD":)\d+(?:\.\d+)?' file
If your numbers can have no integer part and can start with a ., use
grep -oP '(?<="USD":)\d*\.?\d+' file
An optional -:
grep -oP '(?<="USD":)-?\d*\.?\d+' file
See IDEONE demo

Bash - How to replace line that includes '/' characters [duplicate]

This question already has answers here:
sed search and replace strings containing / [duplicate]
(2 answers)
Closed 7 years ago.
I'm trying to replace a line in a csv file that includes '/' characters in one of its columns. Is there a way to replace it with sed? I'm using two variables: one to store the line to replace and another one for the string that will replace it.
sed -i "/s/${CURRENTLINE}/${NEWLINE}/g"
example of the line format:
907;name;2015;4444;DOC;44;user;06/03/2015
Thanks in advance!
Your sed command starts with s and you can use an alternate reges delimiter:
sed -i "s~${CURRENTLINE}~${NEWLINE}~g" file.csv