Put a character before each line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Put a character before each line
# 1  
Old 06-19-2013
Put a character before each line

i have few lines:
Code:
3 4 5 6
#72 9 9 9
4 5 67 8 9
#4 6 8 92 1

i want to put '#' before each line.
i used this
Code:
awk '{print "#" $0;}

it gave
Code:
#3 4 5 6
##72 9 9 9
#4 5 67 8 9
##4 6 8 92 1

but i want the output like this:
Code:
#3 4 5 6
#72 9 9 9
#4 5 67 8 9
#4 6 8 92 1

I want to avoid the character if it's present already. could you pls tell me the command?

Last edited by radoulov; 04-03-2014 at 12:22 PM..
# 2  
Old 06-19-2013
How about and edit that removes the leading # if it exists and then add a # to every line?

Does that suggest anything?



Robin
Liverpool/Blackburn
UK
# 3  
Old 06-19-2013
Code:
sed '/^#/ ! s/^/#/' /path/to/input

This s/^/#/ puts a "#" in front of a line. This: /^#/ ! makes sure this operation is only carried out on lines not starting with "#".

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 06-19-2013
A slightly different version:

Code:
sed 's/^[^#]/#&/' infile

Prefix every line that starts with a character different than # with #.
This User Gave Thanks to radoulov For This Post:
# 5  
Old 06-19-2013
Quote:
Originally Posted by radoulov
A slightly different version:

Code:
sed 's/^[^#]/#&/' infile

Prefix every line that starts with a character different than # with #.
Code:
awk ' { sub("^[^#]","#&") } 1 '  file

This User Gave Thanks to anbu23 For This Post:
# 6  
Old 06-19-2013
Thanks all of u.....got the desired output
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to put a 80 character limit on a long topic line in markdown?

I have a topic line in markdown that spans more than 80 characters that i need to add a line break. Markdown is simply treating the line break as a brand new line instead of continuing as a topic line. Eg: # This is a very long line Markdown interprets it as This is a very long line (4 Replies)
Discussion started by: dragonpoint
4 Replies

2. Shell Programming and Scripting

Hebrew character convert error while put thru ftp

Hi all , i have a script which cp xml files from linux to other server thru ftp my xml file contains charcters in hebrew . my script is #!/bin/bash HOST="....." USER="....." PASSWORD="..." cd /usr2/app/naama/ filelist='find . -mmin -60 | tail -n +2 | awk -F "/" '{print $2}' | grep xml'... (3 Replies)
Discussion started by: naamas03
3 Replies

3. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

4. Shell Programming and Scripting

Awk: sed help: How to put bracket in the last character.

Hi Experts, A quick question. - How to put a 2nd bracket in the last character of the input string. a=blah or a=1234 or a=anything Desired output to be : bla or 123 or anythin What I have tried is not working: echo $a| sed 's/$//' blah Thanks a lot. (3 Replies)
Discussion started by: rveri
3 Replies

5. Shell Programming and Scripting

Sed: delete on each line before a character and after a character

Hi there, A total sed noob here. Is there a way using sed to delete everything before a character AND after another character on each line in a file? The deletion should also delete the indicating characters(here: an opening and a closing parenthesis). The original file would look like... (3 Replies)
Discussion started by: bnbsd
3 Replies

6. Programming

Macro to put single quotes around character

Dear Mates, I am trying to write a macro to replace with its arguments with singles quotes around each argument. #define DECR(a,b,c,d,e) decr('a','b','c','d','e') if the usage is DECR(k,e,y,s,\0) I want it to be replaced with the following decr('k','e','y','s','\0') However it... (2 Replies)
Discussion started by: tamil.pamaran
2 Replies

7. Programming

Perl - how to put to the next line if I have same pattern in one line

Dear All, Could you help me how to put to the next line if I have pattern below in Perl language. Data-123 Linux MacOSData-124 windows FreeBSDData-125 OpenBSD NetBSD I would the output below: Data-123 Linux MacOS Data-124 windows FreeBSD Data-125 OpenBSD NetBSD ... (2 Replies)
Discussion started by: askari
2 Replies

8. Shell Programming and Scripting

Remove line based on string and put new line with parameter

Hi Folks, I am new to ksh, i have informatica parameter file that i need to update everyday with shell script. i need your help updating this file with new parameters. sample data $$TABLE1_DATE=04-27-2011 $$TABLE2_DATE=04-23-2011 $$TABLE3_DATE=03-19-2011 .......Highligned... (4 Replies)
Discussion started by: victor369
4 Replies

9. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

10. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies
Login or Register to Ask a Question