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



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-30-2008
RRVARMA's Avatar
Registered User
 

Join Date: Feb 2008
Location: Bangalore, INDIA
Posts: 34
Exclamation How to edit a txt file ?

Hi,

I need to edit a text file which is like this..


Code:
-- D3341000600	AGEC901164   XYZ	SE0109	1RNVX	AH	2009-01-19	2009-01-11	2009-01-21
-- D3341000600	AGEC901164   XYZ	SE0109	1RNVX	AH	2009-01-19	2009-01-11	2009-01-21
-- D3341006000	AGEC921472   XYZ	SE0109	1RNVX	AH	2009-01-19	2009-01-11	2009-01-21
-- D3341006000	AGEC921472   XYZ	SE0109	1RNVX	AH	2009-01-19	2009-01-11	2009-01-21
-- D3213716600	DCEC160720   XYZ	RZ9191	1RNVX	AH	2009-01-19	2009-01-11	2009-01-21
-- D3213716600	DCEC160720   XYZ	RZ9191	1RNVX	AH	2009-01-19	2009-01-11	2009-01-21
-- D3213716300	DCEC169043   XYZ	RZ9191	1RNVX	AH	2009-01-19	2009-01-11	2009-01-21
-- D3213716300	DCEC169043   XYZ	RZ9191	1RNVX	AH	2009-01-19	2009-01-11	2009-01-21
-- D3213716100	DCEC197234   XYZ	RZ9191	1RNVX	AH	2009-01-19	2009-01-11	2009-01-21
-- D3213716100	DCEC197234   XYZ	RZ9191	1RNVX	AH	2009-01-19	2009-01-11	2009-01-21

I need to get it like this..


Code:
 D3341000600|AGEC901164   XYZ|SE0109|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
 D3341000600|AGEC901164   XYZ|SE0109|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
 D3341006000|AGEC921472   XYZ|SE0109|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
 D3341006000|AGEC921472   XYZ|SE0109|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
 D3213716600|DCEC160720   XYZ|RZ9191|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
 D3213716600|DCEC160720   XYZ|RZ9191|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
 D3213716300|DCEC169043   XYZ|RZ9191|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
 D3213716300|DCEC169043   XYZ|RZ9191|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
 D3213716100|DCEC197234   XYZ|RZ9191|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
 D3213716100|DCEC197234   XYZ|RZ9191|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21

I tried with this code.. but its not working.. the script is not terminating.. and the log file is empty..


Code:
#!/bin/ksh


for name in `cat data.txt`
do
cat $name |cut -f2,3,4,5,6,7,8,9|tr '\t' '|' > processed_data.txt
done

if [ $? -eq 0] ; then echo " Prcocessing completed."
else echo "script failed"
fi

Please help..

Thanks,
RRVARMA
Sponsored Links
  #2 (permalink)  
Old 05-30-2008
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 4,962
One way:


Code:
awk 'BEGIN {OFS="|"}
{print $2,$3"   "$4,$5,$6,$7,$8,$9,$10}
' file

Regards

Last edited by Franklin52; 05-30-2008 at 09:34 AM.. Reason: add code tags
  #3 (permalink)  
Old 05-30-2008
ahmad.diab's Avatar
Registered User
 

Join Date: May 2008
Location: Amman Jordan in MEA
Posts: 484
Re:

You can do the folowing

first save the folowing code in a file and u can name it as awk1

the code have to be in the same line (all of it) :

/--/{printf"%-11s|%-13s%-3s|%-6s|%-5s|%-2s|%-10s|%-10s|%-10s\n",$2,$3,$4,$5,$6,$7,$8,$9,$10 }

then do the command

awk -f awk1 file > output

BR
  #4 (permalink)  
Old 05-30-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,459
Wink Be careful of spacing

Not sure of your problem, but spacing could have been an issue around certain commands. At any rate, the following command and result is what I just got (I am using bash):

Code:
> cat data.txt | cut -c4-200 | cut -f1-8 | tr "\t" "|"
D3341000600|AGEC901164   XYZ|SE0109|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
D3341000600|AGEC901164   XYZ|SE0109|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
D3341006000|AGEC921472   XYZ|SE0109|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
D3341006000|AGEC921472   XYZ|SE0109|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
D3213716600|DCEC160720   XYZ|RZ9191|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
D3213716600|DCEC160720   XYZ|RZ9191|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
D3213716300|DCEC169043   XYZ|RZ9191|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
D3213716300|DCEC169043   XYZ|RZ9191|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
D3213716100|DCEC197234   XYZ|RZ9191|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21
D3213716100|DCEC197234   XYZ|RZ9191|1RNVX|AH|2009-01-19|2009-01-11|2009-01-21

To send to a file, just add the appropriate command at the end of the line:
cat data.txt | cut -c4-200 | cut -f1-8 | tr "\t" "|" >processed_data.txt
  #5 (permalink)  
Old 05-30-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
You don't cat individual lines of data; cat expects a file name argument. But looping over individual lines is pretty pointless anyway, as both cut and tr can handle multi-line data. Simply replacing tabs with pipes and discarding the first three characters should suffice, right?


Code:
tr '\t' '|' <data.txt | cut -c3- >processed_data.txt

By the by, it's more elegant to run the command inside the if:


Code:
 if tr '\t' '|' <data.txt | cut -c3- >processed_data.txt; then
  echo Success
else
  echo Failure
fi

The printing of a message when the script is already done is not very useful as such; the user will see that the script has finished when the prompt returns, and the exit code will indicate the status. (Neither cut not tr can really fail, unless you have invalid syntax, so the only possible failure I see here is a disk or file system failure.)
Sponsored Links
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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
edit a file using ksh meghana Shell Programming and Scripting 3 04-16-2008 12:04 PM
Edit value in File sewood Shell Programming and Scripting 1 03-18-2008 02:11 AM
How to edit env file duke0001 UNIX for Advanced & Expert Users 3 02-16-2007 11:43 AM
file edit help sentak Shell Programming and Scripting 10 11-14-2006 08:20 AM
Edit an ISO / dd file? WIntellect Filesystems, Disks and Memory 4 11-20-2002 05:21 PM



All times are GMT -4. The time now is 01:09 PM.


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-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0