copy loses the text format


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers copy loses the text format
# 1  
Old 04-13-2008
copy loses the text format

Hi
I try to copy part of text from one file to another file. My problem is the text in the new file loses all the format.

My code is:
#!/bin/sh
while red line
do
if [ "$line" != "THE END" ]
then
echo "$line" >> ./new_file
else
break
fi
done < "./old_file"

Is there a way to modify my code so that the new file keeps the format (i.e. tab) ?

Thanks in advance for any help
# 2  
Old 04-13-2008
You can tweak the IFS but properly using read/echo like this is a bit of a voodoo art when it comes to preserving whitespace and control character formatting.

Code:
#!/bin/sh
IFS='
'  # set IFS to newline (in single quotes)
while red line
do
if [ "$line" != "THE END" ]
then
echo "$line" >> ./new_file
else
break
fi
done < "./old_file"

Could you be persuaded to use, say, sed instead?

Code:
sed -n -e '/^THE END$/q' -e p old_file >new_file

# 3  
Old 04-13-2008
The sed command above can combined as:

Code:
sed -n '/^THE END$/q;p' old_file > new_file

or with awk:

Code:
awk '/^THE END$/{exit}1' old_file > new_file

Regards
# 4  
Old 04-13-2008
Thank you era.
sed works very well
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable loses value outside loop

Hi, I am looking for a global variable found_flag to be set a value that can be accessed outside the loop anywhere in the bash shell script. more test.sh found_flag=0 searchdir=/web/bea_apps/applications find . -type f \! -name tasty.tar | $AWK -F/ '{print $NF}' | while IFS=... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. Shell Programming and Scripting

Copy UNIX File into a DSN with a specific Format

Hi All , I am new to programming and here is what i am trying to achieve , i am taking a list of mounted filesystems (based on the HLQ) , passing it on to a txt file and then copying the file to a DSN .The code i am using : df | grep WAST.*WASCFG.*ZFS | awk '{print $2}' | sort -o log.txt |... (5 Replies)
Discussion started by: AnjanM
5 Replies

3. Shell Programming and Scripting

Need script for transferring bulk files from one format to text format

"Help Me" Need script for transferring bulk files from one format to text format in a unix server. Please suggest (2 Replies)
Discussion started by: Kranthi Kumar
2 Replies

4. Shell Programming and Scripting

Text Format

I'd like to format a text in unix before mailing it to users. I want to change color and font of my plain text in unix. Thanks.. (1 Reply)
Discussion started by: Sara_84
1 Replies

5. Shell Programming and Scripting

text format

Hi all I need you help to create script get the below output Thanks in advance. Ashan name.txt Wymouth_MRI_Lun79 PACS DR TEMP R drive stat.txt NO Normal Normal Out need Wymouth_MRI_Lun79 NO Normal PACS DR TEMP R drive Normal (1 Reply)
Discussion started by: ashanabey
1 Replies

6. Shell Programming and Scripting

Generate and copy files in UNICODE format from Linux to Windows

Hi,We have an interface which extracts data from database tables, spool the records into text files, and copy those files using SFTP to a windows server location. The target system loads these files into its database using some DTS packages in SQL Server. This interface of exporting text files... (13 Replies)
Discussion started by: urjagadeesh
13 Replies

7. Shell Programming and Scripting

How to copy one file into another except some text

I have file input like- abc,"123",""123","123","123","123","123","123","123","123","123","123","123","123", abc,"123","123","123","123","123","123","123","123","123""123",abc etc Input file consists of 1000 records and i want to copy into final file except abc abc is at fixed position... (6 Replies)
Discussion started by: AhmedLakadkutta
6 Replies

8. Shell Programming and Scripting

format text

input file test.csv has following structure. SCHEM1.TAB1;COL1;DATATYPE;NOTNULL-- SCHEMA1.TAB1;COL2;DATATYPE;NOTNULL;WITH DEFAULT;-- .... SCHEMA1.TABn;COL1;DATATYPE;NOTNULL;WITH DEFAULT; i started using awk but text is not tab seperated.its just ; seperated. how to print output... (4 Replies)
Discussion started by: rocking77
4 Replies

9. Shell Programming and Scripting

Script copy text

Good morning every body i have a document html and i would like to extract some parts of texte to repete in the midle of balises it is diferent (blabla) the information to extract,severels times i the same documents <div class="channelTitle"> blablaaaaaaaa ... (2 Replies)
Discussion started by: Novice-
2 Replies

10. Shell Programming and Scripting

Displaying file in html loses format

I have a bash script to output the contents of a text file to html. Everything outputs ok, except this: ###################################################################### # # # PRIVATE/PROPRIETARY # # # # ANY UNAUTHORIZED ACCESS TO, OR MISUSE OF ABC COMPANY # # SYSTEMS OR DATA MAY... (2 Replies)
Discussion started by: numele
2 Replies
Login or Register to Ask a Question