- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Remove columns in file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-13-2007 08:42 AM
тАО11-13-2007 08:42 AM
Remove columns in file
Please,
I have a file which has some rows and this has 22 columns, separated by tab, however I want to create a new file or in the same one that I take off the head and alone it places 20 columns, of equal it forms separated by tab
Their help with a script that allows me to make this.
Thank you
"Attachment deleted as it appeared to contain priveleged information"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-13-2007 09:56 AM
тАО11-13-2007 09:56 AM
Re: Remove columns in file
# perl -nle 'next if $.==1;@a=split;print join "\t", @a[0..19] file
This will skip the first line of 'file' and output twenty (20) columns of data from each row read using a tab (\t) as a column field seperator. Perl counts zero-relative, hence the [0..19] slice.
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-13-2007 01:43 PM - edited тАО09-18-2011 12:47 PM
тАО11-13-2007 01:43 PM - edited тАО09-18-2011 12:47 PM
Re: Remove columns in file
You can use awk. The character after -F and OFS= is a tab:
awk -F" " -v OFS=" " '
BEGIN { getline } # skip first
{
print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20
} ' filename
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-14-2007 01:29 AM
тАО11-14-2007 01:29 AM
Re: Remove columns in file
This is:
awk -F"\t" -v OFS="\t" '
BEGIN { getline } # skip first
{
print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
$11, $12, $13, $14, $15, $16, $17, $18, $19, $20
} ' casita.txt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-14-2007 01:43 AM
тАО11-14-2007 01:43 AM
Re: Remove columns in file
The Perl solution splits the fields of your input file on "whitespace" which means that either spaces OR tab characters (or both) can delimit your input fields. As you requested, the output field seperators are tab characters (\t).
Of course, the Perl solution is quite short :-))
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-14-2007 09:50 AM
тАО11-14-2007 09:50 AM
Re: Remove columns in file
That I modify in script if I want to maintain the head??
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-14-2007 10:26 AM
тАО11-14-2007 10:26 AM
Re: Remove columns in file
# awk -F'\t' 'NR>1 {for(i=1;i<=20;++i) printf(i<20?"%s\t":"%s\n",$i)}' ofile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-14-2007 10:57 AM
тАО11-14-2007 10:57 AM
Re: Remove columns in file
> That I modify in script if I want to maintain the head??
This will retain the first line ($.==1) of your file in an unaltered state but produce output of twenty tab-seperated fields from the remaining lines:
# perl -nle 'print if $.==1;@a=split;print join "\t", @a[0..19] file
Now, if you want to update your 'file' "in-place" while creating a backup of the original file ('file.old'), do:
# perl -ni.old -le 'print if $.==1;@a=split;print join "\t", @a[0..19] file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-14-2007 11:29 AM
тАО11-14-2007 11:29 AM
Re: Remove columns in file
For awk, if you don't want to truncate the extra columns:
BEGIN { getline; print $0 } # print first
...
If every column title is tab delimited and you want to delete the name, just remove that BEGIN and process the headings as normal data.
>JRF: the Perl solution is quite short :-))
>Sandman: Yet another way of doing the same thing:
The reason I spelled everything out in awk is to allow future more complex changes and be more maintainable.
If our answers were helpful please read the following about assigning points:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33
Also if no further questions you should close the thread.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2007 09:46 PM
тАО11-28-2007 09:46 PM
Re: Remove columns in file
BEGIN { getline } # skip first
{
print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
$11, $12, $13, $14, $15, $16, $17, $18, $19, $20
} '
intLine=`cat casota.txt | wc -l`
tail -${intLine} | awk -F"\t" -v OFS="\t" '
BEGIN { getline } # skip first
{
print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
$11, $12, $13, $14, $15, $16, $17, $18, $19, $20
} '
I have given the shell script, you can use it. Here I assume your file is casota.txt.
Modify according to your requirement.
Rgds
-NKG-