Move lines containing numbers to new file [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 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

Related

multiple sed in for loop with specific input [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 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"

grep an element in a df and display only selected columns with bash [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
Hel lo I have a df file such as
col1;col2;col3;col4
A;B;C;D
E;F;G;H
I;J;K;L
and I would like to grep I and only display the col1 and col2
and get
I;J then
because from now I only know how to do :
grep 'I' df.csv
I;J;K;L
Try this:
grep 'I' df.csv | cut -d';' -f1-2
The cut command will treat each input line as a list of fields separated by ; (-d';'), and will select only the first two fields (-f1-2) for output.
Sample session:
$ cat df.csv
col1;col2;col3;col4
A;B;C;D
E;F;G;H
I;J;K;L
$ grep 'I' df.csv | cut -d';' -f1-2
I;J
$

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

CLI - How to select a file or directory with special encoding? [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 4 years ago.
Improve this question
Instead of copying and pasting it on the command line.
Is there a way to go through the files, or select them via ID's or something?
You can include a numeric id to your list of files with:
ls | nl
For example if you have this files in your directory:
file1
�%9Cbersicht.html
file2
then ls | nl will give:
1 file1
2 �%9Cbersicht.html
3 file2
from here you can use grep and awk to do other tasks like copying. Example:
ls | nl | grep '\<2\>' | awk '{ print $2 }'
�%9Cbersicht.html # prints the name of the file
or print the file contents:
cat $(ls | nl | grep '\<2\>' | awk '{ print $2 }')

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)}'

Resources