Adding additional characters while preserving original text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding additional characters while preserving original text
# 1  
Old 11-18-2010
Adding additional characters while preserving original text

Hi Forum.

I'm struggling on this relatively easy request to add additional 4 0's to an existing text in a file (whenever I see the pattern -# where # represents any number) using sed command while preserving the rest of the text in the files.

Code:
Original Text:
[s_m_f_acct_key_meas_daily]
$DBConnection_EDW=SAS2EDW
$$startdate=to_char(sysdate-9, 'j')
$$enddate=to_char(sysdate, 'j')

[s_m_f_acct_txn]
$PMSessionLogFile=$PMRootDir/Logs/AUTO.S_M_F_ACCT_TXN.LOG
$DBConnection_EDW=SAS2EDW
$$startdate=to_char(sysdate-10,'j')
$$enddate=to_char(sysdate, 'j')

Final Text:
[s_m_f_acct_key_meas_daily]
$DBConnection_EDW=SAS2EDW
$$startdate=to_char(sysdate-90000, 'j')
$$enddate=to_char(sysdate, 'j')

[s_m_f_acct_txn]
$PMSessionLogFile=$PMRootDir/Logs/AUTO.S_M_F_ACCT_TXN.LOG
$DBConnection_EDW=SAS2EDW
$$startdate=to_char(sysdate-100000,'j')
$$enddate=to_char(sysdate, 'j')

Any help is greatly appreciated.

Thank you.
# 2  
Old 11-18-2010
try this,
Code:
sed 's/sysdate-\(.*\),/sysdate-\10000,/g' inputfile

# 3  
Old 11-18-2010
Code:
sed 's/\(sysdate-[0-9]*\)/&000/' file

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 11-18-2010
Thanks Franklin52 for your quick response.

In my data, it's not always "sysdate-", it can be like this:

Code:
[s_m_f_mtg_res_ead_lgd_monthly_v1]
$PMSessionLogFile=$PMRootDir/Logs/AUTO.LOG
$DBConnection_EDW=SAS2EDW
$$startdate=to_char(TRUNC(LAST_DAY(ADD_MONTHS(SYSDATE,-2)))+1,'j')
$$enddate=to_char(sysdate, 'j')

so I'm running your suggested code but modified slightly to sed where there's a dash followed by a number:

sed 's/\(-[0-9]*\)/&000/' file

Issue that I have is the code is also converting the comments as follows:

Original Text:
--
-- Paramfile: SAS_FACT1.param
--

New Text:
-0000-
-0000- Paramfile: SAS_FACT1.param
-0000-

Can code be modified slightly to only add 4 0's if a dash is immediately followed by a number and ignore comments (--)?

Thank you.
# 5  
Old 11-18-2010
How about this,
Code:
sed 's/-\([0-9]\+\)/-\10000/g'  inputfile

# 6  
Old 11-18-2010
Quote:
Originally Posted by pchang
Thanks Franklin52 for your quick response.

In my data, it's not always "sysdate-", it can be like this:

Code:
[s_m_f_mtg_res_ead_lgd_monthly_v1]
$PMSessionLogFile=$PMRootDir/Logs/AUTO.LOG
$DBConnection_EDW=SAS2EDW
$$startdate=to_char(TRUNC(LAST_DAY(ADD_MONTHS(SYSDATE,-2)))+1,'j')
$$enddate=to_char(sysdate, 'j')

Code:
sed 's/\(sysdate-[0-9]*\)/&000/;s/\(SYSDATE,-[0-9]*\)/&000/' file

Quote:
Originally Posted by pchang
Original Text:
--
-- Paramfile: SAS_FACT1.param
--

New Text:
-0000-
-0000- Paramfile: SAS_FACT1.param
-0000-
Code:
sed 's/^--/-0000-/' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable being stored with additional characters

I am trying to store a string value to a temporary variable within my script. When I run the command manually it returns the expected value, but from the script an additional "\r" is at the end. tCurrentStatus=`cat ${tPC_status} | grep 'Current status' | awk '{print $NF}'` output: cat... (1 Reply)
Discussion started by: jxrst
1 Replies

2. Red Hat

Adding Additional Capacity with megacli

I'm trying to add 6 more hard drives to my RAID array, none of the drives are foreign, they won't be replacing any drives either. I just need to add them to the RAID array. I can't seem to get them added, what am I missing? ---------- Post updated 08-03-12 at 12:28 PM ---------- Previous... (0 Replies)
Discussion started by: eccentricson
0 Replies

3. Shell Programming and Scripting

Adding an additional blank field to a file

Hi, I have the following file, I'd like to add an additional blank field to this file This is a tab delimited file, I have tried the same thing on excel, but looking for a unix solution. Here is my input: Country Postal Admin4 StreetBaseName StreetType HUN 2243 Kóka Dózsa György ... (3 Replies)
Discussion started by: ramky79
3 Replies

4. AIX

Any Additional Steps After Adding New RAM To Sever?

Hi All, We have a server at a client site running AIX 5.3, which we just up the RAM to 32GB, from initially 16GB (if I'm not mistaken). This server is our Application server running J2EE applications on top of Oracle Internet Application Server. Recently we encountered one of the batch jobs... (12 Replies)
Discussion started by: a_sim
12 Replies

5. Shell Programming and Scripting

Help in preserving special characters from input file

Hi Forum. I've tried to search online for a solution but I cannot seem to find one. Hopefully, someone here can help me out. I would appreciate it. Input file abc.txt: $InputFile_Borrower=CMTSLST\EDW_COMMERCIAL_MTGE_BORROWER_dat.lst... (14 Replies)
Discussion started by: pchang
14 Replies

6. Shell Programming and Scripting

Adding text to file on certain lines with(special characters)

I need to add "new lines" of text with special characters, to specific lines in the file. There are 3 modifications needed. Been testing 2 here without success. #!/usr/bin/perl use FileHandle; $file=FileHandle->new; $FILENAME="/opt/etc/usr/file.txt"; $file->open ("<$FILENAME") or die... (13 Replies)
Discussion started by: A4ron4perl
13 Replies

7. Shell Programming and Scripting

Adding additional column at the end

I have a file like below. The separator between reconds is ">" Each record consists of 2 numbers separated by a space. I want to write an awk script that copies the second number and puts it in the third column. :rolleyes: > 10 0 13 5.92346 16 10.3106 19 13.9672 22 16.9838 25... (5 Replies)
Discussion started by: kristinu
5 Replies

8. UNIX for Advanced & Expert Users

adding additional drive to sco the is xenix

I am taking an old xenix drive and installing it in a recent SCO Build Server. I have gone through the process of running mkdev hd twice since the drive is a SCSI then proceed to run mkdev fs and when I attempt to add one of the shown partitions I get the following: fsck: cannot determine... (1 Reply)
Discussion started by: justenglabs
1 Replies

9. UNIX for Dummies Questions & Answers

Adding an additional harddrive in solaris 9

Hello, I have a system which a new harddrive was installed for additional space. I now need to mount the drive and transfer data from /home to the new drive with a mount point named /home. How do I go about doing this? Thanks in advance. (5 Replies)
Discussion started by: GLJ@USC
5 Replies
Login or Register to Ask a Question