Search specific word and delete it from file in linux - linux

I have a file (file1.txt) which contains below text:
mon
tue
tue_day
tuesday
wed
and I want to search for a word "tue" and delete it from this file.
I used
sed -i "/tue/d" file1.txt
but it deletes all the lines containing tue word i.e. line 2,3 and 4. I want to delete the only line 2 which conatins exact same text that i want to remove from file.
could you please suggest?

Just tell sed that you want lines that are exactly "tue". How? Prepending and appending ^ and $ to indicate beginning and end of line:
$ sed '/^tue$/d' file
mon
tue_day
tuesday
wed
To replace with something given in a variable, use double quotes like this:
var="tue"
sed -i "/^$var$/d" file

Related

fIm trying to find a string betwen two match patterns and then add that string before a pattern using sed

Say I have the below line in file named "logs_test":
Sample input:
"at 10947 usecs after Tue Feb 23 18:29:46 2021 [119] init: Event=populatedonRestart"
I wanted to find a string between "at" and "usecs" and add the string before "2021" in the above line
sample output:
"at 10947 usecs after Tue Feb 23 18:29:46 10947 2021 [119] init: Event=populatedonRestart"
sed command to find a string between two matching patterns:
sed "s/at//;s/usecs.*//“ <file_name>
sed command to add a string before a pattern:
sed 's/2021/string &/g' <file_name>
How can I accomplish two tasks using one sed command? Is there were to use the sed command inside sed to do this ?
This will do it for your example (with GNU sed):
sed 's/^\(at \)\(.* \)\(usecs.*\)\(2021.*\)/\1\2\3\2\4/' your_file
The ways it's working is as follows:
I remember the stuff in between \( and \) (these are called capture groups)
I break the string into 4 capture groups, the 2nd capture group is the
number you care about. And I put the groups back together and use the 2nd
capture group twice: /\1\2\3\2\4/
Once you've confirmed it does what you want, you could add the -i to do the
replacement in-place:
sed -i 's/^\(at \)\(.* \)\(usecs.*\)\(2021.*\)/\1\2\3\2\4/' your_file

Unix: Using sed remove part of each line

I am trying to remove the Who: ,What: ,When: ,Where: and the proceeding space from each line in a text file. Below is a example:
Who: Tester1+Password
What: Authentication Success
When: Tues March 20, 2015 08:15:02 UTD
Where: 198.192.1.2
If you want to remove anything upto first :, then you can do:
sed -re 's/(^[^:]+: )(.*)/\2/' file
Tester1+Password
Authentication Success
Tues March 20, 2015 08:15:02 UTD
198.192.1.2
As Glenn suggested we can avoid capture groups completely by removing the portion we don't need.
sed 's/[^:]\+: //' file
Use cut to exclude the first space-separated field:
cut -d " " -f 2- file
sed 's/^[^ ]* *//' YourFile
assuming only these 4 word are possible and as your sample
Put the pattern which matches What, where, when , who inside a capturing group followed by a colon. Then replace the matched chars with an empty string. Add i flag at the last if you want do a case-insensitive match.
$ sed 's/^Wh\(ere\|en\|at\|o\):[[:blank:]]*//' file
Tester1+Password
Authentication Success
Tues March 20, 2015 08:15:02 UTD
198.192.1.2
For general case, you could use
sed 's/^[^[:blank:]]\+[[:blank:]]\+//' file

shell command delete line in text file with specific text in line

In looking for a command to delete a line (or lines) from a text file that contain a certain string.
For example
I have a text file as follows
Sat 21-12-2014,10.21,78%
Sat 21-12-2014,11.21,60%
Sun 22-12-2014,09.09,21%
I want to delete all lines that have "21-12-2014" in them.
I'm not able to find a solution that works.
According to #twalberg there is more three alternate solution for this question, which I'm explaining is as follows for future reader of this question for more versatile solutions:
With grep command
grep -v 21-12-2014 filename.txt
explanations:
-v is used to find non-matching lines
With awk command
awk '! /21-12-2014/' filename.txt
explanations:
! is denoting it will print all other lines that contain match of the string. It is not operator signify ignorance.
With sed command
sed -e '/21-12-2014/d' < filename.txt
explanations:
-e is signify scripted regex to be executed
d is denoting delete any match
< is redirecting the input file content to command
Try doing this :
sed -i.bak '/21-12-2014/d' *
A bit of explanations :
sed : the main command line, put the mouse pointer on sed
-i.bak : replace the file in place and make a backup in a .bak file
// is the regex
d means: delete

Replace Text with Formatted Text on Linux

I'm looking for a way to replace the text between positions 10 and 17 in a file with a Linux command or script. For example, I'd like to replace the date text 20140101 with 01/01/2014.
I'm hoping this is something I can in a single command from the command line with maybe sed or awk?
Using sed, you can capture the first 9 chars in a capture group that would be placed as is. The remaining would be broken in 3 capture groups and re-arranged as you desire.
Something like:
sed -r 's#(.{9})(.{4})(.{2})(.{2})#\1\3/\4/\2#' file
If you are on a system that does not have GNU sed escape ( ) { } with \.
If all your strings are dates then the best is to use date command:
$ date -d 20140101 +%m/%d/%Y
01/01/2014
$ date -d 20140923 +"%m-%d-%Y %a %b"
09-23-2014 Tue Sep
It is especially great tool to translate number of seconds from 1970 (unix epoch) used in many log files:
$ date --date='#1411199063' +"%m-%d-%Y %H:%M:%S"
09-20-2014 07:44:23

How to search a specific column in a file for a string in shell?

I am learning shell scripting. I wrote a script that indexes all the files and folders in the current working directory in the following tab separated format,
Path Filename File/Directory Last Modified date Size Textfile(Y/N)
for example, two lines in my file 'index.txt' are,
home/Desktop/C C d Thu Jan 16 01:23:57 PST 2014 4 KB N
home/Desktop/C/100novels.txt 100novels.txt f Thu Mar 14 06:04:06 PST 2013 0 KB Y
Now, i want to write another script to search for files from the previous file that takes in command line parameters, the file name. And also optional parameters as -dir (if to search only for directories), -date DATE (for last modified date), -t (for text files only) etc. How to go about this ?
I know the grep command that searches in a file for a string, but suppose i entered a file name, it would search the entire file for the file name instead of searching only the filename column. How do i do this? Is there any other command to achieve this ?
"i want to search in a speific column for a particular string... how to do that ?"
For example, to search for directories:
awk -F '\t' '$3 == "d"' filename
You can also search by regular expression or substring matching. Please post further questions as you learn.
Single quotes prevent variable substitution (and other forms of expansion) -- see the bash manual. So you have to pass the value by other means:
awk -F'\t' -v s="$search_file" '$2 == s' ./index.txt > final.txt
or, use double quotes, more fragile and harder to read IMO
awk -F'\t' "\$2 == \"$search_file\"" ./index.txt > final.txt
Also, you don't mix single and double quotes. Pick one or the other, depending on the functionality you need.
You can doit using the command awk. For example to print the first column of a file using awk:
awk -F ":" '{print $1}' /etc/passwd
(note that the -F flag is to choose a field separator for the columns, in your case you don't need it). Then you can use grep to filter in that column. Good luck!

Resources