multiple sed in for loop with specific input [closed] - bash

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a for loop with inside two sed commands to edit different files. My problem is the second sed that should edit only the files named aaa but instead I get the error sed cannot edit "not a regular file".
for file in $directory
do [-e "$file"] || continue
sed -i 's/hello/hello1/g' "$file"
sed -i 's/hallo/hello12/g' [[ "$file" ~ aaa ]]
done

I suggest to replace
[-e "$file"]
with
[ -e "$file" ]
and
sed -i 's/hallo/hello12/g' [[ "$file" ~ aaa ]]
with
[[ "$file" =~ ^aaa$ ]] && sed -i 's/hallo/hello12/g' "$file"

Related

Move lines containing numbers to new file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have list
dasfdsaf
afddt4fd
asdw5h6ufdh
sfds2dsddf2nu
dsfnjufsd443ajisdfaij
sdfjid654sij
asfdnu7sdfui
sdfmii
I want to move all lines containing numbers in it to new file.
If your grep has the -P option:
grep -P '\d' old_file > new_file
grep -vP '\d' old_file > temp
mv temp old_file
If -P isn't supported, use [0-9] instead of \d to match digits:
grep '[0-9]' old_file > new_file
grep -v '[0-9]' old_file > temp
mv temp old_file

How do I print out the longest word in a file that appears at least 10 times [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Exactly what the title says: what is the bash command for printing out the longest word in a text file that appears at least 10 times.
Try this Denis:
tr -s " " "\n" < file | while read -r l; do echo "${#l} $l"; done | sort -n | awk '$1 >= 10 ' | awk '{print $2}' | tail -n1

how to pass text file as argument in shell script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
the text file contains
from:sender#gmail.com
to:receiver#gmail.com
subject:
attachment:asdfg.xlsx
all arguments should be handled in shell script
I tried but if subject contains space then it gives problem
from=$(echo $1|cut -d ":" -f 2 <<< "$1")
to=$(echo $2|cut -d ":" -f 2 <<< "$2")
subject="$3"
attachment=$(echo $4|cut -d ":" -f 2 <<< "$4")
When you could read the Input_file then passing it as a variable will not be a good option IMHO, so create variables inside script by reading Input_file, opting OP's method of creating variables but enhancing code to awk.
from=$(awk -F':' '/from/{print $NF}' Input_file)
to=$(awk -F':' '/^to/{print $NF}' Input_file)
subject=$(awk -F':' '/^subject/{if($NF){print $NF} else {print "NULL subject"}}' Input_file)
attachment=$(awk -F':' '/^attachment/{print $NF}' Input_file)

How to crop a word [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have this list of inputs :
imalex
thislara
heiscarl
how to get :
alex
lara
carl
grep
Use grep to take the last four chars:
grep -o '.\{4\}$' file
The -o option makes sure only matched parts are printed.
sed
Using sed we can achieve a similar result:
sed 's/.*\(.\{4\}\)$/\1/' a
Here we capture the last four digits and replace each line with those last four digits. They are captured in a group \( \) and inserted \1.
read & tail
We can also grab the last five chars (including the newline) of each line using tail and a -c option. We do that for each line using read.
while read line; do
tail -c 5 <<< $line
done < file
2 answers using substring arithmetic
bash:
while read word; do
echo "${word:${#word}-4}"
done <<<"$list"
awk
echo "$list" | awk '{print substr($NF, length($NF)-4+1)}'

Trying to create so many .dat files using loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My requirement is to create files in the respective directory (existing) in the server.
#!/bin/ksh
file1=$1 # Signifies DATE config file
file2=$2 # Signifies MONT config file
file4=$3 # Signifies SEQN config file
config1=`cat $file1 | awk -F "," '{print $1}'`
config2=`cat $file2 | awk -F "," '{print $2}'`
config3=`cat $file3 | awk -F "," '{print $3}'`
echo AT_EXTENSION_"$config1""$config2""$config3".dat
cat MY_DIR/$echo
Will this KornShell (ksh) script work to create a file in the respective directory with the contents in echo?
As the question currently stands, it appears that you want to write the contents of files $file1, $file2, and $file3 into a file called AT_EXTENSION_"$config1""$config2""$config3".dat. If this is the case, then do:
cat "$file1" "$file2" "$file3" >AT_EXTENSION_"$config1""$config2""$config3".dat
You should try to not use cat with awk and you should change back tics to parentheses $(code) and also add double quote around the variable name, to make sure file is read correctly
Not like this:
config1=`cat $file1 | awk -F "," '{print $1}'`
But like this:
config1=$(awk -F "," '{print $1}' "$file1")

Resources