Formatting of file to an desired one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Formatting of file to an desired one
# 1  
Old 04-01-2008
Formatting of file to an desired one

Hi,

I am trying to format an existing file to the desired format.

Existing file : abc.txt
2332 |3322 |5534 |5436 |NULL |

Desire file :
QTD GROSS|2332 |3322 |5534 |5436 |


echo "QTD GROSS|" > out.xt
cat abc.txt | cut -c1-52 >> out.txt

I even tried using tee command, but couldn't succeed.
From above script i am getting it as :

QTD GROSS|
2332 |3322 |5534 |5436 |

Instead of
QTD GROSS|2332 |3322 |5534 |5436 |

Can you suggest me how to append the file contents as desired.

Thanks,
Praveen.
# 2  
Old 04-01-2008
echo gives a newline character after a line, use printf instead of echo with the first line.

Code:
printf("QTD GROSS|") > out.xt

Regards
# 3  
Old 04-01-2008
Or:

Code:
echo "QTD GROSS|$(cat abc.txt | cut -c1-52)" > out.txt

However, if your file is going to be more than one line, neither of these is going to work. Then you'll need something more like

Code:
OLDIFS=${IFS}
IFS="
"
while read LINE
do
   echo "QTD GROSS|$(print ${LINE} | cut -c1-52)"
done < abc.txt > out.txt
IFS=${OLDIFS}

# 4  
Old 04-01-2008
Quote:
Originally Posted by pkondur
Hi,

I am trying to format an existing file to the desired format.

Existing file : abc.txt
2332 |3322 |5534 |5436 |NULL |

Desire file :
QTD GROSS|2332 |3322 |5534 |5436 |

If there's only one line in the file:
Code:
IFS=\| read a b c d e < abc.txt
printf "QTD GROSS|%s|%s|%s|%s" "$a" "$b" "$c" "$d"

If there are more lines (but not too many):
Code:
while IFS=\| read a b c d e
do
  printf "QTD GROSS|%s|%s|%s|%s" "$a" "$b" "$c" "$d"
done < abc.txt

If it's a large file, use awk:
Code:
awk -F\| 'printf "QTD GROSS|%s|%s|%s|%s", $1, $2, $3, $4' abc.txt

# 5  
Old 04-01-2008
Maybe this one
Code:
awk -F"|" '{for(i=1;i+1<NF;i++){a=a $i "|"} print "QTD GROSS|"  a}' abc.txt > out.txt

# 6  
Old 04-02-2008
Thanks for your replies

Hi Every one,

Thanks for your suggestions.

The below one helped me to format.

echo "QTD GROSS|`(cat abc.txt | cut -c1-52)`" > out.txt

I am not that Familiar with awk command usage. Will try to put in use others too.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Needed shell script to append desired text to each line in a file

Hi, I had generated a report in my tool as followsoutput.txt 43.35 9 i needed the script to generate a new file like below i want to append the text to each of these lines of my filenewoutputfile.txt should be Total Amount : 43.35 Record Count:9 Regards, Vasa Saikumar. ... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

3. UNIX for Dummies Questions & Answers

How to print the line from the file in the desired format:?

I am reading a file of Linux ( like mentioned below) & the data is represented in a single line like mentioned below: 11/03 4:00 39992 0.098 5.195 0.034 0.001 1.091 182 0.000 0 0.071 4.252 0.033 0.001 666.53 Now i want to print the result in other file something like this :- 39992... (5 Replies)
Discussion started by: Anamica
5 Replies

4. Emergency UNIX and Linux Support

Script to fill the file system mount with empty files to desired size

We are regularly using for our testing, where we are manually filling up the mount with desired size with following command dd if=/dev/zero of=file_2GB bs=2048000 count=2000 We are planning to automate the task where taking input for % of size as one input and the name of the file system... (8 Replies)
Discussion started by: chandu123
8 Replies

5. UNIX for Dummies Questions & Answers

Help with saving file in desired format

Hi I have the following file in the unix named emp. ID,NAME,SAL,DEPT 101,aaa,2000,10 102,bbb,3000,20 103,ccc,4000,30 104,ddd,5000,40 105,aaa,2000,50 106,bbb,3000,60 107,ccc,4000,70 108,ddd,5000,10 109,aaa,2000,80 I need to save first 3 columns(ID, NAME, SAL) in another file with... (2 Replies)
Discussion started by: alok3141
2 Replies

6. Shell Programming and Scripting

handling CSV file to get desired output

Hi All , i have a CSV file , pattern is given below :- Group # name # host # account # stop # # start # # check -------------------------------------------------------------------------- file format and data exmaple :- RBP2,RB0112,sihrb001,tksrb011,. ./.profile 1>/dev/null 2>&1;stop_olc_dmn... (0 Replies)
Discussion started by: deepakiniimt
0 Replies

7. Shell Programming and Scripting

File created in a different location instead of desired location on using crontab

Hi, I am logging to a linux server through a user "user1" in /home directory. There is a script in a directory in 'root' for which all permissions are available including the directory. This script when executed creates a file in the directory. When the script is added to crontab, on... (1 Reply)
Discussion started by: archana.n
1 Replies

8. Shell Programming and Scripting

Newbie.. Find if a file exists and open, if not create the desired file..

Hey all, I'm brand new to script writing, I'm wanting to make a script that will ask for a file and then retrieve that file if it exists, and if it doesn't exist, create the file with the desired name, and I'm completely stuck.. so far.. #! bin/bash echo "Enter desired file" read "$file" if ... (5 Replies)
Discussion started by: Byrang
5 Replies

9. Shell Programming and Scripting

HELP: I need to sort a text file in an uncommon manner, can't get desired results

Hi All I have a flat text file. Each line in it contains a "/full path/filename". The last three columns are predictable, but directory depth of each line varies. I want to sort on the last three columns, starting from the last, 2nd last and 3rd last. In that order. The last three columns... (6 Replies)
Discussion started by: JakeKatz
6 Replies

10. UNIX for Advanced & Expert Users

get the timestamp of a file in desired format

Hi, I have a file say abc. I get the timestamp in following way: ls -ltr abc | awk -F" " '{print $6,$7,$8}' Mar 8 10:23 I need to get the timestamp as : 03-08-2007 10:23:00 Thanks Sumeet (1 Reply)
Discussion started by: sumeet
1 Replies
Login or Register to Ask a Question