How to edit a txt file ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to edit a txt file ?
# 1  
Old 05-30-2008
Error 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
# 2  
Old 05-30-2008
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  
Old 05-30-2008
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  
Old 05-30-2008
Hammer & Screwdriver 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  
Old 05-30-2008
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.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

I can't edit a txt file shared from Window server to Solaris

Hello, I'm working with Solaris and I need to share files between Windows Server 2012 and Solaris through an authentication with an Active Directory user, where Windows Server is the "main server". All was going good, until after see the WS files and I tried to open one and modify it, but I cannot... (1 Reply)
Discussion started by: QeratD0
1 Replies

2. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

3. Shell Programming and Scripting

To transpose columns + edit in a txt file

Hi, I have a txt file that looks like log2FoldChange Ontology_term 8.50624450251828 GO:0003700,GO:0003707,GO:0005634,GO:0006355,GO:0043401,GO:0003700,GO:0005634,GO:0006355,GO:0008270,GO:0043565 7.03936870356684 GO:0005515,GO:0008080 6.49606183738682 6.49525073909629 GO:0005515... (4 Replies)
Discussion started by: alisrpp
4 Replies

4. Shell Programming and Scripting

Need to append the date | abcddate.txt to the first line of my txt file

I want to add/append the info in the following format to my.txt file. 20130702|abcd20130702.txt FN|SN|DOB I tried the below script but it throws me some exceptions. <#!/bin/sh dt = date '+%y%m%d'members; echo $dt+|+members+$dt; /usr/bin/awk -f BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies

5. Shell Programming and Scripting

awk append fileA.txt to growing file B.txt

This is appending a column. My question is fairly simple. I have a program generating data in a form like so: 1 20 2 22 3 23 4 12 5 43 For ever iteration I'm generating this data. I have the basic idea with cut -f 2 fileA.txt | paste -d >> FileB.txt ???? I want FileB.txt to grow, and... (4 Replies)
Discussion started by: theawknewbie
4 Replies

6. Shell Programming and Scripting

command to list .txt and .TXT file

Hi expersts, in my directory i have *.txt and *.TXT and *.TXT.log, *.txt.log I want list only .txt and .TXT files in one command... how to ?? //purple (1 Reply)
Discussion started by: thepurple
1 Replies

7. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies

8. Shell Programming and Scripting

Edit txt file using vi editor

Dear All I have a file called sample.txt which contains as follows HR Files records Loaded RecordDate Unloaded -- ---- -------- --------- ------------ ------ 00 567 77777 67896 0 0 01 345 345567 45678 0 ... (1 Reply)
Discussion started by: tkbharani
1 Replies

9. AIX

How to edit txt file by shell script?

What I want to do is just delete some lines from a text file, I know it's easy using copy and redirect function, but what I have to do is edit this file (delete the lines) directly, as new lines may be added to the text file during this period. Can AIX do this ? # cat text 1:line1 2:line2... (3 Replies)
Discussion started by: dupeng
3 Replies

10. UNIX for Dummies Questions & Answers

Edit txt?

Hi! A windows user going UNIX on part time, oh no?!! So my question is how I edit and save documents in UNIX? In DOS you can type 'edit x.txt' to both create a new file and edit a file that already exists. How do I do this in Unix? (2 Replies)
Discussion started by: <Therapy>
2 Replies
Login or Register to Ask a Question