CLI - How to select a file or directory with special encoding? [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 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 }')

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

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 UPTIME from port number in UNIX command [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
Is it possible to get the server UPTIME from mentioning PORT number or Process ID in UNIX command? Please help! Thanks!
From port - I don't think so.
From process ID :
ps -eo pid,etime | grep " 1 " | grep -v grep | awk '{print $2}'
Example:
root#localhost:~[0]$ ps -eo pid,etime | grep " 1 " | grep -v grep | awk '{print $2}'
01:16:10
root#localhost:~[0]$
But you can use uptime command.

How to filter records from a file using bash 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 8 years ago.
Improve this question
I'm supposed to filter records from multiple files. Files are delimited by |. On the 24th field, I will filter the records by "9120". How am I supposed to filter the files by using bash script?
20|09328833007|0|0|9222193385|0|GS|515051032704315|0|*HOME||20140311|101640|0|0|‌​||12|18|0|0|1||3100|00886FC0||0|0|| |||N|N|||N||||N|||||| 301131301|||||||||||11|0||00|FF|11|FF|140311101640|352912058113130000||CEBS1|MSH‌​15
The more concise way using awk:
awk '$24=="9120"' FS='|' file*
Using variable input:
awk -v col=24 -v value="9120" '$col==value' FS='|' file*
awk is useful to processing files like this. You set the FIELD SEPARATOR to |:
To print the 24th field:
$ awk -F '|' '{ print $24 }' sample.txt
3100
To print lines where the 24th field is the value you specified:
awk -F '|' '$24=="9120" { print; }' sample.txt
Try this:
cat file* | awk -F"|" '$24=="9120" {print $0}'

Resources