awk - how to delete first column with field separator
Assuming your original CSV file is named "orig.csv":
awk -F'|' '{print $1 > "newfile"; sub(/^[^|]+\|/,"")}1' orig.csv > tmp && mv tmp orig.csv
awk: change field separator keeping first column as is
Could you please try following, written and tested on shown samples only. This should work with any number of fields too tested it in https://ideone.com/fWgggq
awk '
BEGIN{
FS="_"
OFS=","
print "ID,group1,group2,group3"
}
FNR>1{
val=$0
$1=$1
print val,$0
}' Input_file
Explanation: Adding detailed explanation for above.
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of program from here.
FS="_" ##Setting field separator as _ here,
OFS="," ##Setting OFS as comma here.
print "ID,group1,group2,group3" ##Printing header as per OP requirement here.
}
FNR>1{ ##Checking condition if this is greater than 1st line then do following.
val=$0 ##Store current line into var val here.
$1=$1 ##reassign first field to itself so that new OFS which is , is implemented to whole line.
print val,$0 ##Printing current new line here.
}' Input_file ##Mentioning Input_file name here.
Unix , Delete first column using awk
Use cut
:
cut -d, -f2- file
-d,
uses ,
as delimiter, and -f2-
prints fields 2 to last.
Remove first columns delimiter
$ awk '{for(i=1;i<4;i++)sub(/;/,"")}1' file
38528;9;5;8;6
7456;2;2;4;6
How to delete first three columns in a delimited file
@Heng: try:
awk -F"|" '{for(i=4;i<=NF;i++){printf("%s%s",$i,i==NF?"":"|")};print ""}' Input_file
OR
awk -F"|" '{for(i=4;i<=NF;i++){printf("%s%s",$i,i==NF?"\n":"|")};}' Input_file
you could re-direct this command's output into a file as per your need.
EDIT:
awk -F"|" 'FNR==1{++e;fi="REPORT_A1_"e;} {for(i=4;i<=NF;i++){printf("%s%s",$i,i==NF?"\n":"|") > fi}}' Input_file1 Input_file2 Input_file3
Delete first column of csv file
Following awk will be helping you in same.
awk '{sub(/[^,]*/,"");sub(/,/,"")} 1' Input_file
Following sed may also help you in same.
sed 's/\([^,]*\),\(.*\)/\2/' Input_file
Explanation:
awk ' ##Starting awk code here.
{
sub(/[^,]*/,"") ##Using sub for substituting everything till 1st occurence of comma(,) with NULL.
sub(/,/,"") ##Using sub for substituting comma with NULL in current line.
}
1 ##Mentioning 1 will print edited/non-edited lines here.
' Input_file ##Mentioning Input_file name here.
Remove first column without removing header
All you need is:
awk 'NR>1{sub(/[^\t]*\t/,"")}1' file
awk -cut how to delete second column with field separator
If your Input_file is same as shown sample then following may help you on same:
awk '{sub(/\|\|o\|\|[0-9]+\|\|o\|\|/,"||o||")} 1' Input_file
Output will be as follows:
'&(||o||lonyfoe||o||Joe||o||Joe||o||Otieno
Remove first columns then leave remaining line untouched in awk
first as ED commented, you have to use FS
as field separator in awk.tab
becomes space
in your output, because you didn't define OFS
.
awk 'BEGIN{FS=OFS="\t"}{$1=$2=$3="";print}' file
this will remove the first 3 fields, and leave rest text "untouched"( you will see the leading 3 tabs). also in output the <tab>
would be kept.
awk 'BEGIN{FS=OFS="\t"}{print $4,$5,$6}' file
will output without leading spaces/tabs. but If you have 500 columns you have to do it in a loop, or use sub
function or consider other tools, cut, for example.
Related Topics
Get a Browser Rendered HTML+Javascript
Nuget on Linux: Error Getting Response Stream
Can't Clone a Github Repo on Linux via Https
Meaning of Tilde in Linux Bash (Not Home Directory)
How to Compile/Install Node.Js(Could Not Configure a Cxx Compiler!) (Ubuntu)
How to Perform a For-Each Loop Over All the Files Under a Specified Path
How to Monitor the Thread Count of a Process on Linux
How to Use Xargs to Copy Files That Have Spaces and Quotes in Their Names
Optimize PDF Files (With Ghostscript or Other)
Centos 64 Bit Bad Elf Interpreter
Git Status Ignore Line Endings/Identical Files/Windows & Linux Environment/Dropbox/Meld
How to Use Arrow Keys Alone to Expand Tree Node in Package Explorer in Eclipse on Linux
Create Zip File and Ignore Directory Structure
Linux Shell Script to Add Leading Zeros to File Names