The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
deleting specific lines from all files in a directory vrms UNIX for Dummies Questions & Answers 3 04-25-2008 11:08 AM
deleting rows & columns form a csv file code19 Shell Programming and Scripting 2 03-13-2008 10:06 AM
searching text files on specific columns for duplicates Gerry405 UNIX for Dummies Questions & Answers 2 08-18-2005 10:51 AM
Deleting specific lines in a file ramu_1980 Shell Programming and Scripting 4 02-15-2005 01:41 PM
Deleting specific files greater then 90 days beilstwh Shell Programming and Scripting 2 07-22-2004 03:27 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-13-2006
premar premar is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 8
Deleting specific columns from a file

Hi Friends,
I want to delete specific columns from a file.
Say my file content is as follows:

"1","a","ww1",1234"
"2","b","wwr3","2222"
"3","c","erre","3333"

Now i want to delete the column 2 and 4 from this file.

That is I want the file content to be:
"1","ww1"
"2","wwr3"
"3","erre"

This file contains lakhs of records.
How is this possible in shell scrpit.

Regards,
Prema
  #2 (permalink)  
Old 02-13-2006
vino's Avatar
vino vino is online now Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
You can awk.

Code:
awk -F, '{ print $1","$3 }' input.txt > output.txt
  #3 (permalink)  
Old 02-13-2006
premar premar is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 8
Thank you

Hi Vino
Thank you
  #4 (permalink)  
Old 02-13-2006
premar premar is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 8
Deletion of specified columns

The code you have mentioned will copy the specified columns of the file to a new file.
Is there any way to delete the specified columns from existing file?
  #5 (permalink)  
Old 02-13-2006
stobo stobo is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 10
cat file | cut -d',' -f1,3
  #6 (permalink)  
Old 02-13-2006
vino's Avatar
vino vino is online now Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Quote:
Originally Posted by premar
The code you have mentioned will copy the specified columns of the file to a new file.
Is there any way to delete the specified columns from existing file?
AFAIK, awk cannot do a read and write on the same file. You have to make use of the temporary file and then rename it to the original file.

You can use either perl or sed if you dont want the redirection of output to a different file.

The sed solution might not very portable. Because, not every sed has a -i option. You would need GNU sed for that.

Code:
sed -i -e 's_\(.*\),.*,\(.*\),.*_\1,\2_g' input.txt
  #7 (permalink)  
Old 02-13-2006
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
  
 

Join Date: May 2002
Location: India
Posts: 295
same way you could alternatively use perl
perl -pie ...... rest same as the above sed command
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0