Using awk to print all columns from the nth to the last
Print all columns:
awk '{print $0}' somefile
Print all but the first column:
awk '{$1=""; print $0}' somefile
Print all but the first two columns:
awk '{$1=$2=""; print $0}' somefile
Printing everything except the first field with awk
Assigning $1
works but it will leave a leading space: awk '{first = $1; $1 = ""; print $0, first; }'
You can also find the number of columns in NF
and use that in a loop.
How to replace text in file between known start and stop positions with a command line utility like sed or awk?
Use this Perl one-liner with substr
and tr
. Note that this uses the fact that you can assign to substr
, which changes the original string:
perl -lpe 'BEGIN { $from = 29; $to = 39; } (substr $_, ( $from - 1 ), ( $to - $from + 1 ) ) =~ tr/ /B/;' in_file > out_file
To change the file in-place, use:
perl -i.bak -lpe 'BEGIN { $from = 29; $to = 39; } (substr $_, ( $from - 1 ), ( $to - $from + 1 ) ) =~ tr/ /B/;' in_file
The Perl one-liner uses these command line flags:-e
: Tells Perl to look for code in-line, instead of in a file.-p
: Loop over the input one line at a time, assigning it to $_
by default. Add print $_
after each loop iteration.-l
: Strip the input line separator ("\n"
on *NIX by default) before executing the code in-line, and append it when printing.-i.bak
: Edit input files in-place (overwrite the input file). Before overwriting, save a backup copy of the original file by appending to its name the extension .bak
.
how to print Lines Between Two Patterns in file using SED or AWK?
You may use this sed
:
sed -n '/MULTIPLE-RESOURCES/,/^###$/ { /###$/!p; }' file
### MULTIPLE-RESOURCES
#### Viewing Resource Information
> kubectl get svc, po
> kubectl get deploy, no
> kubectl get all
> kubectl get all --all-namespaces
## KUBECTL
How to fetch a particular string using a sed command
EDIT: As per OP's change of Input_file and OP's comments, adding following now.
awk '
BEGIN{ FS="|"; OFS="," }
{
sub(/[^:]*:/,"",$1)
gsub(/^[^<]*|; .*/,"",$4)
gsub(/^<|>$/,"",$4)
print $1,$4
}' Input_file
With shown samples, could you please try following, written and tested with shown samples in GNU awk
.
awk '
BEGIN{
FS="|"
OFS=","
}
{
val=""
for(i=1;i<=NF;i++){
split($i,arr,":")
if(arr[1]=="a" || arr[1]=="d"){
gsub(/^[^:]*:|; .*/,"",$i)
gsub(/^<|>$/,"",$i)
val=(val?val OFS:"")$i
}
}
print val
}
' Input_file
Explanation: Adding detailed explanation for above.
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of this program from here.
FS="|" ##Setting FS as pipe here.
OFS="," ##Setting OFS as comma here.
}
{
val="" ##Nullify val here(to avoid conflicts of its value later).
for(i=1;i<=NF;i++){ ##Traversing through all fields here
split($i,arr,":") ##Splitting current field into arr with delimiter by :
if(arr[1]=="a" || arr[1]=="d"){ ##Checking condition if first element of arr is either a OR d
gsub(/^[^:]*:|; .*/,"",$i) ##Globally substituting from starting till 1st occurrence of colon OR from semi colon to everything with NULL in $i.
val=(val?val OFS:"")$i ##Creating variable val which has current field value and keep adding in it.
}
}
print val ##printing val here.
}
' Input_file ##Mentioning Input_file name here.
Related Topics
200,000 Images in Single Folder in Linux, Performance Issue or Not
Why Can't We Use C Standard Library Functions in Kernel Development
Coreos - Get Docker Container Name by Pid
Shifting from Windows to *Nix Programming Platform
"Cd" Does Not Work in Shell Script
Extract List of File Names in a Zip Archive When 'Unzip -L'
Source Code of Pthread Library
Remove Occurrences of String in Text File
Check If File Is Open with Lsof
How to Configure the Linux Kernel Within Buildroot
Delete Whitespace in Each Begin of Line of File, Using Bash
Ubuntu Equivalent of Yum's Whatprovides, to Find Which Package Provides a File
How to "Interconnect" Two Sockets in Linux
How to Use .Notparallel in Makefile Only on Specific Targets
Linker Cannot Find Symbols, But Libraries Are Read and Symbols Exist