Need to add a numeric & special char to end of the first line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to add a numeric & special char to end of the first line
# 1  
Old 11-12-2016
Need to add a numeric & special char to end of the first line

Need to add a numeric & special char to end of the first line

Existing file:
Code:
12-11-16|11 2016 Jan 12:34:55|03:55|
13-10-16|10 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55|
14-10-16|19 2016 Jan 12:34:55|03:55|13-11-16|11 2016 Jan 12:34:55|04:55|
15-10-16|18 2016 Jan 12:34:55|03:55|14-11-16|11 2016 Jan 12:34:55|05:55|
16-10-16|17 2016 Jan 12:34:55|03:55|15-11-16|11 2016 Jan 12:34:55|06:55|
17-10-16|16 2016 Jan 12:34:55|03:55|16-11-16|11 2016 Jan 12:34:55|07:55|

I have used the below command to perform that, but it is working only for string
Code:
VAR="12-11-16|11 2016 Jan 12:34:55|03:55|"

sed -i '1s/$/"$VAR"/' file

when am using this for above $VAR which has special char in it, it is failing, is there a way to do this in awk or in sed without redirecting to another file?
# 2  
Old 11-12-2016
Quote:
Originally Posted by Joselouis
Need to add a numeric & special char to end of the first line
Existing file:
Code:
12-11-16|11 2016 Jan 12:34:55|03:55|
13-10-16|10 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55|
14-10-16|19 2016 Jan 12:34:55|03:55|13-11-16|11 2016 Jan 12:34:55|04:55|
15-10-16|18 2016 Jan 12:34:55|03:55|14-11-16|11 2016 Jan 12:34:55|05:55|
16-10-16|17 2016 Jan 12:34:55|03:55|15-11-16|11 2016 Jan 12:34:55|06:55|
17-10-16|16 2016 Jan 12:34:55|03:55|16-11-16|11 2016 Jan 12:34:55|07:55|

I have used the below command to perform that, but it is working only for string
Code:
VAR="12-11-16|11 2016 Jan 12:34:55|03:55|"
sed -i '1s/$/"$VAR"/' file

when am using this for above $VAR which has special char in it, it is failing, is there a way to do this in awk or in sed without redirecting to another file?
Hello Joselouis,

Welcome to forums, hope you will enjoy learning and sharing knowledge and learning here. Could you please try following as a starting point and let me know if this helps you.
Code:
sed -i '1s/^/'"$s1"'/;1s/$/'"$s1"'/'  Input_file

Where variable named s1have the value of " and it will provide the output as follows.
Code:
sed -i '1s/^/'"$s1"'/;1s/$/'"$s1"'/'  Input_file

Output will be as follows.
Code:
"12-11-16|11 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55|"
13-10-16|10 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55|
14-10-16|19 2016 Jan 12:34:55|03:55|13-11-16|11 2016 Jan 12:34:55|04:55|
15-10-16|18 2016 Jan 12:34:55|03:55|14-11-16|11 2016 Jan 12:34:55|05:55|
16-10-16|17 2016 Jan 12:34:55|03:55|15-11-16|11 2016 Jan 12:34:55|06:55|
17-10-16|16 2016 Jan 12:34:55|03:55|16-11-16|11 2016 Jan 12:34:55|07:55|

NOTE: Though we could use -ioption of sed but it is not good practice to use it with large size files, it is better to use redirect option of standard output to a temp file and then rename it to the orginal Input_file.

Thanks,
R. Singh

Last edited by RavinderSingh13; 11-12-2016 at 10:22 AM.. Reason: Addeda note here also with solution now too.
# 3  
Old 11-12-2016
Hi Singh,

Thanks for your reply, sorry it didn't work out, it again failed because of the special characters in it.
# 4  
Old 11-12-2016
Hello Joselouis,

Could you please try command as follows to check if there are garbage characters inside your Input_file.
Code:
cat -v Input_file

If you see Control M characters in your Input_file then you could anyone of the following options to remove them.

i- use dos2unix utility to remove control M(garbage characters) inside Input_file(Also your box should have utility installed if not as a root uer you could do it.)
ii- use tr command, just an example as follows too.
Code:
tr -d '\r' < Input_file

iii- Use awk's subsitutation functionality i.e as follows(haven't tested though).
Code:
awk '{gsub('\r',X,$0);print}' Input_file

Once you are done with removing the garbage characters in your Input_file and then you could my suggestion provided into POST#2. kindly do so and do let me know if this helps you.

Thanks,
R. Singh
# 5  
Old 11-12-2016
Hi,

I don't think it is problem with quotes in sed instead of variable declaration.

Here is the example:
Code:
A="Hi"; echo $A
Hi
A=\"Hi\"; echo $A
"Hi"
A="\"12-11-16|11 2016 Jan 12:34:55|03:55|\""
echo "13-10-16|10 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55|" | sed -e "s/$/$A/"

Gives output:
Quote:
13-10-16|10 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55|"12-11-16|11 2016 Jan 12:34:55|03:55|"
If this is not your expected output, please post your input , expected output and variable value. Always write what was the error / output deviation from desired output etc .

@ Ravinder : you can use sed -i.bak to create a backupfile (.bak or any extension) first before making substitution in original file automatically ( and can avoid interim file , rename etc ) .
This User Gave Thanks to greet_sed For This Post:
# 6  
Old 11-12-2016
In the below input file, end of the first line I want to add the below variable value
Code:
VAR="12-11-16|11 2016 Jan 12:34:55|03:55|"

Input_file:
12-11-16|11 2016 Jan 12:34:55|03:55|
13-10-16|10 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55|
14-10-16|19 2016 Jan 12:34:55|03:55|13-11-16|11 2016 Jan 12:34:55|04:55|
15-10-16|18 2016 Jan 12:34:55|03:55|14-11-16|11 2016 Jan 12:34:55|05:55|
16-10-16|17 2016 Jan 12:34:55|03:55|15-11-16|11 2016 Jan 12:34:55|06:55|
17-10-16|16 2016 Jan 12:34:55|03:55|16-11-16|11 2016 Jan 12:34:55|07:55|

Desire output:
Code:
12-11-16|11 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55|
13-10-16|10 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55|
14-10-16|19 2016 Jan 12:34:55|03:55|13-11-16|11 2016 Jan 12:34:55|04:55|
15-10-16|18 2016 Jan 12:34:55|03:55|14-11-16|11 2016 Jan 12:34:55|05:55|
16-10-16|17 2016 Jan 12:34:55|03:55|15-11-16|11 2016 Jan 12:34:55|06:55|
17-10-16|16 2016 Jan 12:34:55|03:55|16-11-16|11 2016 Jan 12:34:55|07:55|

Command and error:
Code:
sed -i "1s/$/"$VAR"/" Input_file 
Error: sed: -e expression #1, char 20: unknown option to `s'

Hi Singh, I tried your three fixes and didn't work

Hi Greet sed, yes that is not my expected out, I have given my expected output above (tried with escap "\" and didnt work out) and want to edit in a file, I want to add the
Code:
$VAR

value in end of the first line
# 7  
Old 11-12-2016
Quote:
Originally Posted by Joselouis
In the below input file, end of the first line I want to add the below variable value
Code:
VAR="12-11-16|11 2016 Jan 12:34:55|03:55|"

Input_file:
12-11-16|11 2016 Jan 12:34:55|03:55|
13-10-16|10 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55|
14-10-16|19 2016 Jan 12:34:55|03:55|13-11-16|11 2016 Jan 12:34:55|04:55|
15-10-16|18 2016 Jan 12:34:55|03:55|14-11-16|11 2016 Jan 12:34:55|05:55|
16-10-16|17 2016 Jan 12:34:55|03:55|15-11-16|11 2016 Jan 12:34:55|06:55|
17-10-16|16 2016 Jan 12:34:55|03:55|16-11-16|11 2016 Jan 12:34:55|07:55|

Desire output:
Code:
12-11-16|11 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55|
13-10-16|10 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55|
14-10-16|19 2016 Jan 12:34:55|03:55|13-11-16|11 2016 Jan 12:34:55|04:55|
15-10-16|18 2016 Jan 12:34:55|03:55|14-11-16|11 2016 Jan 12:34:55|05:55|
16-10-16|17 2016 Jan 12:34:55|03:55|15-11-16|11 2016 Jan 12:34:55|06:55|
17-10-16|16 2016 Jan 12:34:55|03:55|16-11-16|11 2016 Jan 12:34:55|07:55|

Command and error:
Code:
sed -i "1s/$/"$VAR"/" Input_file 
Error: sed: -e expression #1, char 20: unknown option to `s'

Hi Singh, I tried your three fixes and didn't work

Hi Greet sed, yes that is not my expected out, I have given my expected output above (tried with escap "\" and didnt work out) and want to edit in a file, I want to add the
Code:
$VAR

value in end of the first line
You have too many quotes (leaving the expansion of $VAR unquoted. Change:
Code:
sed -i "1s/$/"$VAR"/" Input_file

to:
Code:
sed -i "1s/$/$VAR/" Input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. SCO

Grep to ignore suffix & find end of line

In COBOL, a hyphen can be used in a field name and in a specific program some field names would be identical to others except a suffix was added--sometimes a suffix to a suffix was used. For example, assume I am looking for AAA, AAA-BBB, and AAA-BBB-CCC and don't want to look at AAA-BBB-CCC... (7 Replies)
Discussion started by: wbport
7 Replies

2. Shell Programming and Scripting

Parsing of Char and Numeric in a file

Hi All, i'm working on some report and currently have this plain text file generated. server_name1|sdfd1deal | 1048572| 1040952| 99| 207| 1| 1 server_name1|dba1dbs | 83886048| 40730796| 48| 5762| 22764| 8... (4 Replies)
Discussion started by: fedora132010
4 Replies

3. Shell Programming and Scripting

Add new line after ' char

Hello experts, I need to convert one file into readable format. Input file is like following line. STG,12345,000,999,' PQR, 2345,000,999,' XYZ,7890,000,999, Output should be following (After ' char new line char should be added.) STG,12345,000,999,' PQR, 2345,000,999,' ... (16 Replies)
Discussion started by: meetmedude
16 Replies

4. Shell Programming and Scripting

Add char before certain line

I'm trying to add a '1' before a line that has the word "C_ID" s/.*C_ID.*/&\n1/ The above code did what I need, but the '1' is added after the C_ID word :( Help please. (5 Replies)
Discussion started by: newbeee
5 Replies

5. UNIX Desktop Questions & Answers

add char o end of line if dosent exist

hey , i want to check if the char "#" exist at the end of every line of txt file and if it dosent then add it for example: the cat jumped on my mom # cars can run on water# i cant get a date blue yellow# will be: the cat went back home# cars can run on water# i cant get a... (2 Replies)
Discussion started by: boaz733
2 Replies

6. Shell Programming and Scripting

Replace new line with <br /> & escape special characters

Hi, I wish to replace a new line with br (html) but it doesn't seem to work message=$(echo ${FORM_message} | tr '\r' '<br \/>' ) what it gives me seems to be ... b...? I am also having problem escaping hash sign in cut command: list=$(echo "$line" | cut -d'\#;\#' -f1) ; my intention is... (2 Replies)
Discussion started by: ted_chou12
2 Replies

7. Shell Programming and Scripting

Remove special char from end of the file

Hi I am working on a bash script and would know how to use cut or sed to remove (F/.M/d h) from a text file. Before 1 text to save (F/.M/d h) after 1 text to save Thanks in advance (5 Replies)
Discussion started by: pelle
5 Replies

8. Shell Programming and Scripting

Converting Char to Numeric

HI, Here I have the following output and want to do some mathematical calculation on C2 & C3 column. c1 c2 c3 c4 c5 l1 1-oct 12:30:01 12:35 abc xyz l2 1-oct 14:20:01 14:35 def ... (5 Replies)
Discussion started by: dear_abhi2007
5 Replies

9. Shell Programming and Scripting

Add end of char \n on end of file

Hi, I want to add \n as a EOF at the end of file if it does't exist in a single command. How to do this? when I use command echo "1\n" > a.txt and od -c a.txt 0000000 1 \n \n 0000003 How does it differentiate \n and eof in this case? Regards, Venkat (1 Reply)
Discussion started by: svenkatareddy
1 Replies

10. Shell Programming and Scripting

Adding a special character at the end of the line

I used following to add * at the end of the line in file1. It adds * at the end but has a space before it for some lines but some other lines it adds exactly after the last character. How do I take out the space ? sed 's/$/*/' file1 > file2 example: contents of file1 : ... (2 Replies)
Discussion started by: pitagi
2 Replies
Login or Register to Ask a Question