How to delete lines in a text file if they start with a number using a shell script - shell

Id like to know a good way to read a file and delete all lines of text that start with numbers, i.e [0-9] using some shell scripting. Thanks in advance!

You could use something like this:
grep -v '^[0-9]' input-file > output-file

Using sed:
sed '/^[0-9]/d' file > outfile
If you have GNU sed, the original file itself can be updated:
sed -i '/^[0-9]/d' file

Related

Delete all strings that do not contain any uppercase in Bash

I need to delete from a file all the words that do not contain any uppercase in bash.
I use the sed command but the output is the same as the input:
I tried sed 's/[^0-9]*//' file
Example input:
sjasd
ksaLK
asdn
Asdw
Output
ksaLK
Asdw
Could you please try following.
sed -n '/[A-Z]/p' Input_file
As per #PaulHodges's comment, once you are happy with results use sed -i .... option in above code to make changes in Input_file itself.
To make a file without those:
grep '[A-Z]' infile > outfile
This is a nondestructive way to check first. Then you could replace the old file with the new one.
If you really want to edit the existing file in place:
sed -i '/[A-Z]/!d' infile
This says to delete all lines that do not have a capital letter.

bash, tool to change line in file

Is it possible with the use of some linux tool change lines in file like following:
was:
status:<whatever>
become:
status:"red"
UPDATE
The best solution:
awk 'BEGIN{FS=OFS=":"} $1 ~ /status/ {$2="\"red\""}1' file
This will print output and we can redirect it to file or whatever.
sed is the Stream EDitor, its purpose is to edit text streams, not text files. It really is the wrong tool for the job here. You should use a text file editor such as ed instead:
ed -- /path/to/file <<-HERE
,s/^status:.*/status:"red"
w
q
HERE
With GNU sed:
sed -E 's/^(status:).*/\1"red"/' file
With a string:
s="red"
sed -E 's/^(status:).*/\1"'"${s//\//}"'"/' file
If you want to edit your file "in place" use sed's option -i.
If your Input_file is same as shown sample then following may help you in same.
echo "status:<whatever>" | sed 's/\(.*:\)\(.*\)/\1"red"/g'

how to remove <Technology> and </Technology> words from a file using shell script?

My text file contains 100 lines and the text file surely contains Technology and /Technology words .In which ,I want to remove Technology and /Technology words present in the file using shell scripting.
sed -i.bak -e 's#/Technology##g' -e 's#Technology##g' my_text_file
This is delete the words and also make a backup of the original file just in case
sed -i -e 's#/Technology##g' -e 's#Technology##g' my_text_file
This will not make a backup but just modify the original file
You can try this one.
sed -r 's/<[\/]*technology>//g' a
Here is an awk
cat file
This Technology
More here
Mine /Technology must go
awk '{gsub(/\/*Technology/,"")}1' file
This
More here
Mine must go
By adding an extra space in the regex, it will not leave an extra space in the output.
awk '{gsub(/\/*Technology /,"")}1' file
This Technology
More here
Mine must go
To write back to original file
awk '{gsub(/\/*Technology /,"")}1' file > tmp && mv tmp file
If you have gnu awk 4.1+ you can do
awk -i '{gsub(/\/*Technology /,"")}1' file

I'm trying to use sed as a replacement for grep

The command I'm trying to use is
sed -n 's/'$LASTNAME'/pgIw '$TEMP_FILE2'' < "$TEMP_FILE"
My goal is to get search TEMP_FILE for the value in LASTNAME and write the line containing the match, if there is one, it to TEMP_FILE2. I keep getting that the sed command is garbled. The code above will return
sed: command garbled: s/smith/pgIw /tmp/tmp.aKaGFH
Any help is appreciated! I've been trying to figure this out for hours! This is suppose to be done in the Korn shell in UNIX and I can't use awk or python, it is the stipulations of the homework.
Thank you!
It looks like you have an abundance of quotes that you don't need. Try:
sed -n "/$LASTNAME/p" >$TEMP_FILE2 <$TEMP_FILE
Also, your use of the s sed command seems to be out of place, since you don't actually want to substitute anything.
If you just want to find something and pipe to output, then simply use grep
grep -i "$LASTNAME" "$TEMP_FILE" > "$TEMP_FILE2" # -i case-insensitive
Or Perl
perl -ne "print if /$LAST_NAME/" "$TEMP_FILE" > "$TEMP_FILE2"

sed command to take variable

I have file File1.txt in which i have to replace a text using sed command
File1.txt contents
EURAMOUNTTOBEREPLACED
I have a AIX shell script for replacing the text AMOUNTTOBEREPLACED
Contents of the shell script
sum=27
sed 's/AMOUNTTOBEREPLACED/"$sum"/g' File1.txt >> temp
mv temp FileNew.txt
After executing the script, the contents of FileNew.txt is as below
EUR"$sum"
But Expected output should be
EUR27
Please help how to do?
I think the one you want is like this:
sed 's/AMOUNTTOBEREPLACED/'$sum'/g' File1.txt >> temp
Basically single qouting takes the string to sed and skips shell which is wrong. You want the shell to interpret the variable so thats what i did. And further if you happen to have gnu version of sed. Then you can do
sed -i 's/AMOUNTTOBEREPLACED/'$sum'/g' File1.txt
which compressed these two statement in your code to one in above:
sed 's/AMOUNTTOBEREPLACED/'$sum'/g' File1.txt >> temp
mv temp FileNew.txt
use the below sed command instead:
sed -e "s/AMOUNTTOBEREPLACED/\"${sum}\"/g"

Resources