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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Append text at end of the first line in a file catgovind Shell Programming and Scripting 7 05-01-2008 08:33 AM
Append to end of each line of file without a temp file. rorey_breaker Shell Programming and Scripting 4 04-03-2008 11:11 AM
append a line to the last line in a file subhrap.das UNIX Desktop for Dummies Questions & Answers 5 04-25-2007 10:17 AM
Append a field to the end of each line of a file based on searching another file. ultimate Shell Programming and Scripting 2 03-29-2005 11:21 AM
Large file need to append to each line r1500 UNIX for Dummies Questions & Answers 1 10-14-2003 11:48 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-20-2008
dsshishya dsshishya is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 21
Question How to Append a Value to each line of the file

Hi,

We have Multiple source files which has some data, I need a K shell script which will append number '1' as the last character to all the lines of the first file and then increments the value and appends '2' to all the lines to the next file and so on.

For Example

Incoming file 1

a,aa,aa,a,aa
b,bb,bb,b,bb

Incoming file 2

c,cc,cc,c,cc
d,dd,dd,d,dd

the Output should be

Incoming file 1

a,aa,aa,a,aa,1
b,bb,bb,b,bb,1

Incoming file 2

c,cc,cc,c,cc,2
d,dd,dd,d,dd ,2


Can anyone Plz help me on this.

Thanks
Sunny.
  #2 (permalink)  
Old 03-20-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,311
Can you explain what you have so far, and what you're having trouble with? Post your sample script, and we'll see how we can assist.

Regards
  #3 (permalink)  
Old 03-20-2008
dsshishya dsshishya is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 21
I am getting the header and detail records in the same file. they dont have a common key. what i am supposed to do is to break each file depending on a fixed header rows and then load them to a seperate file, But we need a Common key to join them in the ETL so the Number i wanted to append at the end of each record will act as a reference and helps me to do that. I am able to split the File in to 2 parts depending on the no of header records.

but where i am stuck is i have to append a number at the end of each line in a file and that number needs to increment for each file

Below is my code

#Arguments
DIRECTORY=$1
FILE=$2
NO_HEADER=$3

#Validating the Arguments

if [ $# -lt 1 ]
then
echo
echo "\n Error :$0 Usage Input File \n"
echo
exit 1

# splits up the Header File

head -$NO_HEADER $DIRECTORY/$FILE > $DIRECTORY/${FILE}.head

echo `cat $DIRECTORY/${FILE}.head | awk 'BEGIN {FS = "," } ; { printf ("%s,", $2) }'` > $DIRECTORY/${FILE}.Header

rm -f $DIRECTORY/${FILE}.head

#calculates the remaining records in the File and writes them into the Detail file

NO_TAIL=`expr $NO_LINES - $NO_HEADER`

tail -$NO_TAIL $DIRECTORY/$FILE > $DIRECTORY/${FILE}.detail

Please help
  #4 (permalink)  
Old 03-20-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,311
Wink to put something to the end of every line

assuming that you have already done a
var_dat="1" or whatever logic you desire

>sed "s/$/$var_dat/" sample.txt >output.txt

this will put the value of $var_dat at the end of every line of sample.txt, creating a new file output.txt
  #5 (permalink)  
Old 03-20-2008
dsshishya dsshishya is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 21
This will work if i am working with a single file, But i have multiple files and the variable value should increment for each file isn't it?
  #6 (permalink)  
Old 03-20-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,311
Try this if awk is allowed, it creats the files "newfile.1", "newfile.2", "newfile.3" and so on.

Code:
awk '
!infile{infile=FILENAME;nr=1}
infile!=FILENAME{infile=FILENAME;close("newfile."nr);nr++}
{print $0","nr >> "newfile."nr}
' file1 file2 file3
Regards
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 09:24 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