How to crop a word [closed] - bash

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

Related

Concatenate the output of multiple cuts in one line 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
I am trying to concatenate the output of three cut functions with - in a shell script in a single line. I tried as below, but wont work. How do I do this ?
echo "$(cut -d',' -f2 FILE.csv)-$(cut -d',' -f1 FILE.csv)-$(cut -d',' -f3 FILE.csv)"
Using awk to change the delimiter:
awk -F, '{ print $2, $1, $3 }' OFS='-' FILE.csv
Or with csvkit commands (Especially useful if your file has more complex CSV with things like commas in quoted fields or multi-line fields that a naive split on comma can't handle correctly):
csvcut --columns 2,1,3 FILE.csv | csvformat -D'-'

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

How to number output in list in bash? [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
I want to number each line that gets outputted when i list a directory, so that instead of typing out the full name of the object, i can identify it with a number in the list. In Bash.
Ex. os-list is a directory I use to store numerous of objects that are ever changing.
os1.xxx.iso is the object name.
From
ls os-list
os1-xxx.iso
os2-xxx.iso
What is the path?: os1-xxx.iso
To
ls os-list
[1]os1-xxx.iso
[2]os2-xxx.iso
What is the path? 1
What is the term that im looking for this kind of operation in bash?
The command select can be used:
files=$(ls os-list)
select choice in ${files[#]}; do
break
done
echo "${choice}"
You can modify this to your needs, just look for more examples with select.
I would change the prompt (PS3="What is the path: ") and replace the break in the select loop (check for a valid response).
I want to number each line that gets outputted when i list a directory
For your exact desired output format:
ls | nl | sed 's/^[ \t]*//' | sed -r 's/^[0-9]*()/[\0]/' | sed 's/\t//'
or
ls | cat -n | sed 's/^[ \t]*//' | sed -r 's/^[0-9]*()/[\0]/' | sed 's/\t//'
If you only want to have number as reference, and not in your exact desired format then simply:
ls | cat -n
or
ls | nl
would suffice, sed pipe is added to enclose given number in square brackets and remove starting/trailing spaces to conform to your desired output. Admitedly, this could be done with awk as well, pipe is not optimized, just given as reference point.
Edit:
with awk like so:
ls | cat -n | awk '{print "[" NR "]"$2}'
Selecting filename based on index (example with index 12 given):
ls | cat -n | awk '{print "[" NR "]"$2}' | grep "^\[12\]" | sed 's/^\[12\]//'
Note of caution: this approach supposes that between listing and selecting no file is added (if file is added in between and your sort order is messed up 12th file in listing and 12th file in select might ended not being the same file).

Getting a line number of a specific word from a text file [duplicate]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Is grep capable of providing the line number on which the specified word appears?
Also, is possible to use grep to search for a word starting from some certain line downward?
Use grep -n to get the line number of a match.
I don't think there's a way to get grep to start on a certain line number. For that, use sed. For example, to start at line 10 and print the line number and line for matching lines, use:
sed -n '10,$ { /regex/ { =; p; } }' file
To get only the line numbers, you could use
grep -n 'regex' | sed 's/^\([0-9]\+\):.*$/\1/'
Or you could simply use sed:
sed -n '/regex/=' file
Combining the two sed commands, you get:
sed -n '10,$ { /regex/= }' file
You can call tail +[line number] [file] and pipe it to grep -n which shows the line number:
tail +[line number] [file] | grep -n /regex/
The only problem with this method is the line numbers reported by grep -n will be [line number] - 1 less than the actual line number in [file].
Or You can use
grep -n . file1 |tail -LineNumberToStartWith|grep regEx
This will take care of numbering the lines in the file
grep -n . file1
This will print the last-LineNumberToStartWith
tail -LineNumberToStartWith
And finally it will grep your desired lines(which will include line number as in orignal file)
grep regEX

Resources