How To
Summary
Some useful utilities for cutting sections of text and also pasting them.
Objective
Environment
Steps
There are lots of ways to "cut" fields out of a file
For example to find the numeric GIDs in use
Here is the top of /etc/group
system:!:0:root,pconsole,esaadmin
staff:!:1:ipsec,esaadmin,sshd,fw,gaz,alex,hmc,HMC
bin:!:2:root,bin
For example if we want to list all the numeric Group IDs (gids) they are in the third field, and the delimiter is a colon, so we could
- Use cut to get the third : delimited field
cut -f 3 -d: /etc/group
- Use awk to get the third : delimited field
awk -F: '{print $3}' /etc/group
Again, awk is a very flexible command (in fact a programming language in its own right) there is a man page and there are many resources on the web.
The name comes from the initial letters of the guys who wrote it in 1977:
- Alfred Aho,
- Peter Weinberger
- Brian Kernighan
There are other ways to do this as well.
Catting
If you want to join files together, one after the other, you can use the cat command to concatenate them, as follows:
I have three files here:
peach-gaz:/tmp/demo# ls -1
file1
file2
file3
This is what they contain
peach-gaz:/tmp/demo# cat file1 file2 file3
1
2
3
one
two
three
alpha
beta
gamma
But I guess that most if not all of you know the cat command, don’t you?
Pasting
Have you heard of paste? It merges the files, line by line with a tab delimiter:
peach-gaz:/tmp/demo# paste file1 file2 file3
1 one alpha
2 two beta
3 three gamma
We can specify a delimiter as well:
peach-gaz:/tmp/demo# paste -d : file1 file2 file3
1:one:alpha
2:two:beta
3:three:gamma
This can be very useful.
If I am working on a problem, I often end up with several files and want to merge the data, line by line, for example I might run commands on an HMC to get a list of all the LPARs on a particular Managed Server, then maybe run a command in each of them to get specific information, such as uptime, number of disks etc.
Then I could paste the various files together with a suitable delimiter.
For more information about the paste command, use this syntax:
man paste
Additional Information
You can contact me:
- mailto:gaz@uk.ibm.com
- Twitter @power_gaz
Document Location
Worldwide
Was this topic helpful?
Document Information
Modified date:
03 May 2021
UID
ibm11116267