How to extract the first column from an output line? - linux

When executing
sudo fdisk -l | tail -n 1
the result gives me
/dev/sdb1 * 8064 7669823 3830880 b W95 FAT32
So what I need is to extract just "/dev/sdb1/".
Not exactly just sdb1, whatever device that is listed last. What if I have two flash drives and I need the last one?
I've searching everywhere and I couldn't find how.
Thanks in advance.

sudo fdisk -l | tail -n 1 | awk '{print $1}'
will produce as
/dev/sdb1

Related

display drives with lsblk -o name -lpn , extract string containing "sd"

So I've looked all over the place for what to do here and just find "from file". I am looking to extract from a command output.
Task: display the absolute path names of disks beginning with the sd.
Current progress: displaying absolute path name of disks
[host /]$ lsblk -o name -lpn
/dev/sda
/dev/sda1
/dev/mapper/centos-root
/dev/sda2
/dev/md127
....
Desired output
/dev/sda
/dev/sda1
/dev/sda2
....
I've played around with cut, print, awk and sed.
Got syntax errors or no output.
grep
lsblk -o name -lpn | grep "/dev/sd"
awk
lsblk -o name -lpn | awk '/dev\/sd/{print}'
sed
lsblk -o name -lpn | sed -n '/\/dev\/sd/p'
Output:
/dev/sda
/dev/sda1
/dev/sda2
lsblk accepts arguments, so you can in many cases just as easily say
lsblk -no name -lp /dev/sd?
The ? is just wildcard match of a single character, so sda, sdb, ...
There is a potential pitfall. If you have more than 26 disks or you are working on a system that uses those unique disk identifiers. In which case you need to change the wildcard to an asterisk and filter out only the unique results,
lsblk -no name -lp /dev/sd* | sort -u
Output (for either command):
/dev/sda
/dev/sda1
/dev/sda2
/dev/sda3
/dev/sdb
/dev/sdb1
/dev/sdb2
Try with sed:
lsblk -o name -lpn |sed -n '/\/sd/p'

Count lines of CLI output in linux

Hi have the following command:
lsscsi | grep HITACHI | awk '{print $6}'
I want that the output will be the number of lines of the original output.
For example, if the original output is:
/dev/sda
/dev/sdb
/dev/sdc
The final output will be 3.
Basically the command wc -l can be used to count the lines in a file or pipe. However, since you want to count the number of lines after a filter has been applied I would recommend to use grep for that:
lsscsi | grep -c 'HITACHI'
-c just prints the number of matching lines.
Another thing. In your example you are using grep .. | awk. That's a useless use of grep. It should be
lsscsi | awk '/HITACHI/{print $6}'

how to do this in bash script

in Linux, suppose mount command return this
/dev/sdc1 on /media/ELF (^-^)V type vfat
/dev/sdb1 on /media/PENDRIVE type vfat
all I want to do is get all mount point of my usb disk.
I did that already, using combination of grep and sed I can get these:
/media/ELF (^-^)V
/media/PENDRIVE
the problem is, when I do for loop in bash, those text will become 3 part instead of 2 parts , I mean :
suppose I put the result of those text in LIST
for list in $LIST; do
echo $list
done;
the result of that for loop becomes
/media/ELF
(^-^)V
/media/PENDRIVE
how to handle this issue? or are there any easier ways to get mount point of my usb disk?
Thanks
If you've already extracted the mountpoint paths and the only issue is to process them in a loop:
while read -r mountpoint; do
echo "[$mountpoint]"
done < <(mount | grep /media | grep uhelper=udisks | sed -e 's/\/dev\/.*on //g' -e 's/ type .*//g')

compare mount and /etc/filesystems in AIX

I want to compare the output put of mount command to the /etc/filesystems. Basically we want to validate everything is getting mounted properly as defined in /etc/filesystems after any system change(reboot etc.)
My basic script is:
#!/bin/bash
mountpoint="/vol/test/abc"
if grep -qs "$mountpoint" /etc/filesystems; then
echo "good"
else
echo "bad"
fi
Is this right approach? Please suggest. Also How can I get all the volumes that are being returned by executing mount command?
I think the general approach you want is to first generate a list of all the filesystems that you expect to be mounted, by looking at /etc/filesystems (use some combination of awk, grep, etc. to get just the names).
Then, get the list of filesystems that are actually mounted by running the mount command with no arguments.
Finally, compare the original list with the second list, and make sure nothing is missing.
As z242 suggested:
# Matching lines from /etc/filesystems
sed -n 's%^\(/.*\):%\1%p' /etc/filesystems | sort -o f1
# Matching lines from mount command
mount | tail +3 | awk '{print $2}' | sort -o f2
# Now compare the two
comm -3 f1 f2
Items listed with no indent are those in /etc/filesystems but not mounted. Items listed with an indent are those mounted but not in /etc/filesystems. If you don't care about the latter change comm -3 to comm -23

Tracking a program's progress reading through a file?

Is there a way to figure out where in a file a program is reading from? It seems like might be doable with strace or dtrace?
To clarify the question and give motivation, say I have a 10GB log file and am counting the number of unique lines:
$ cat log.txt | sort | uniq | wc -l
Can I check where in the file cat is currently at, effectively giving the progress of the command? Using lsof, I can't seem to get the offset of last file read, which I think is what would do the trick:
$ lsof log.txt
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
cat 16021 erik 3r REG 0,22 13416118210 1078133219
Edit: I apologize, the example I gave is too narrow and misses the point. Ideally, for an arbitrary program, I would like to see where in the file reads are occurring (regardless of pipe).
You can do what you want with the progress command. It shows the progress of coreutils tools such as cat or other programs in reading their file.
File and offset information is available in Linux in /proc/<PID>/fd and /proc/<PID>/fdinfo.
Instead of cat :
pv log.txt | sort | uniq | wc -l
Piping with pv :
SIZE=$( ls -l log.txt | awk '{print $5}'); cat log.txt | sort | pv -s $SIZE | uniq | wc -l
If the example is truly your use case, then I'd recommend pipe viewer.

Resources