Sponsored Content
Top Forums Shell Programming and Scripting SED: Place char at starting and replace selected line Post 302308962 by hkansal on Monday 20th of April 2009 06:16:15 PM
Old 04-20-2009
Bug SED: Place char at starting and replace selected line

Hello Experts,

I am working on a small file editing script. Since all experts here are very generous to give me the complete code, I would take up the problem in steps so that I ensure my opportunity to learn.

AIM: The script has some commented and some uncommented lines. I need to :
  1. Comment and uncomment lines as per requirement
  2. Change some lines, again as per requirement
  3. The requirement is specified by the command line arg
Here is what I have thought of:
  1. Place # in front of all uncommented lines.
  2. edit the required line
Suppose my file f.txt is:
Code:
#qwertyu
asdfgh
#zxcvbnm
qwertyuiop

The desired o/p is:
Code:
#qwertyu
#asdfgh
#zxcvbnm
#qwertyuiop

What I tried:
Code:
sed 's/^/#/g' f.txt > result.txt             # first command
sed 's/^[^#]/#/g' f.txt > result.txt      # second command

The problem is:
First command places a # in front of each line, not that it's a problem as I am simply concerned with commenting the lines, but I want to know if what I am trying is possible.
Second command replaces the first character too Smilie.

Please provide me some ideas to approach this problem.

If you think I am headed in the wrong direction, pls correct me and advise some other way to accomplish my task.

Kindly avoid direct code solutions, hints are most welcome.

Thank You.

Regards,
HKansal

Last edited by hkansal; 04-21-2009 at 10:44 AM.. Reason: corrected typo
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to print only selected char in a string..?

Hi, I want to print particular chars in a string. for example ie., consider " dear,. roopa$#09%~`';']" as the example string. Here, I want to print only alphanumeric chars.. suppose , if i want only alphanumeric... value would be "dear roopa09" suppose , if i want some spl char(,) with... (2 Replies)
Discussion started by: balan_mca
2 Replies

2. Shell Programming and Scripting

In vi editor I want to replace next line char by space

in vi editor I want to replace next line char by space help me eg: input: 123 123 123 output: 123 123 123 (5 Replies)
Discussion started by: RahulJoshi
5 Replies

3. Shell Programming and Scripting

Delete a line between selected lines using sed or any other command

I want to delete a line between selected lines using sed: e.g. : Between "bus" to "pins", delete lines conaining "signal" word. Input : bus direction signal new signal old pins signal ok end Desired Output: bus direction pins signal end (4 Replies)
Discussion started by: nehashine
4 Replies

4. Shell Programming and Scripting

Place digit in front of the line searching pattern using sed command

hi All, i want to add the single digit front of the line in the report file and string compare with pattern file. patter file: pattern1.txt pattern num like 4 love 3 john 2 report file: report.txt i like very much but john is good boy i will love u so after execute... (9 Replies)
Discussion started by: krbala1985
9 Replies

5. UNIX for Dummies Questions & Answers

UNIX BASH replace char with dash w/o SED

I am trying to create a script in UNIX BASH where I can change the string to dashes. This is part of a "hangman" game (not a homework assignment). Once the dashes are in place, I then need to add the letter to the new string (of dashes) based on where the letter is located. (ie The word is 'String'... (11 Replies)
Discussion started by: needsomehelp
11 Replies

6. Shell Programming and Scripting

replace line starting with not a number

Dear users, I have a file like this: geometry,geometry_vertex_count,Id,strnum,platecode,datatype,dtnum,refnum,appearance,disappeara,color,geogdesc,datatype_ft_style,import_notes "<LineString><coordinates>-130.6539,51.5103,0 -130.7708,51.6287,0 -130.8356,51.6832,0 -130.9211,51.7772,0... (5 Replies)
Discussion started by: Gery
5 Replies

7. Shell Programming and Scripting

sed to delete selected line from file

Ultimate goal is to delete lines from before and after a block of lines in a given file. First attempt was something like this: sed -e '1,/STARTUP/ d' inputfile.txt > outputfile.txt but that deleted everything down to and including the line with STARTUP. I need to delete everything before... (6 Replies)
Discussion started by: edstevens
6 Replies

8. Shell Programming and Scripting

Find and replace in starting of line using sed

Hi, My requirement is, to find the word "data" and replace the starting of each line with # using sed. /dev/datavg/xxx /xxx ext3 defaults 1 2 /dev/datavg/yyy /yyy ext3 defaults 1 2 result to be #/dev/datavg/xxx /xxx ... (1 Reply)
Discussion started by: ksgnathan
1 Replies

9. Shell Programming and Scripting

Match a char with duplicates in a line and replace one of them

Hi, i have a huge file that need to check for a pattern that occur more than once in a line like below:- #lkk>cd-m>A0DV0>192.134.1.1 blablabladsdjsk jshdfskfslfs #lqk>cd-m>A1SV0>192.14.11.1 blalalbnalablab balablablajakjakjakja #pldqw>sf-w>PH67FR>168.55.1.1 balablabala... (5 Replies)
Discussion started by: redse171
5 Replies

10. UNIX for Advanced & Expert Users

Replace \n char true line Separator

Unix File is pipe delimited with 17 fields. We may get extra pipes in data also. We may get \n char (1 or more \n in one field or multi fileds) in data in any field. Need to replace \n true ( line separator) with 'space and bell char space' chars (' \a ') Not data \n. Input:... (1 Reply)
Discussion started by: rajeshkumare
1 Replies
unix2dos(1)						      General Commands Manual						       unix2dos(1)

NAME
unix2dos - UNIX to DOS text file format converter SYNOPSYS
unix2dos [options] [-c convmode] [-o file ...] [-n infile outfile ...] Options: [-hkqV] [--help] [--keepdate] [--quiet] [--version] DESCRIPTION
This manual page documents unix2dos, the program that converts text files in UNIX format to DOS format. OPTIONS
The following options are available: -h --help Print online help. -k --keepdate Keep the date stamp of output file same as input file. -q --quiet Quiet mode. Suppress all warning and messages. -V --version Prints version information. -c --convmode convmode Sets conversion mode. Simulates unix2dos under SunOS. -o --oldfile file ... Old file mode. Convert the file and write output to it. The program default to run in this mode. Wildcard names may be used. -n --newfile infile outfile ... New file mode. Convert the infile and write output to outfile. File names must be given in pairs and wildcard names should NOT be used or you WILL lost your files. EXAMPLES
Get input from stdin and write output to stdout. unix2dos Convert and replace a.txt. Convert and replace b.txt. unix2dos a.txt b.txt unix2dos -o a.txt b.txt Convert and replace a.txt in ASCII conversion mode. Convert and replace b.txt in ISO conversion mode. unix2dos a.txt -c iso b.txt unix2dos -c ascii a.txt -c iso b.txt Convert and replace a.txt while keeping original date stamp. unix2dos -k a.txt unix2dos -k -o a.txt Convert a.txt and write to e.txt. unix2dos -n a.txt e.txt Convert a.txt and write to e.txt, keep date stamp of e.txt same as a.txt. unix2dos -k -n a.txt e.txt Convert and replace a.txt. Convert b.txt and write to e.txt. unix2dos a.txt -n b.txt e.txt unix2dos -o a.txt -n b.txt e.txt Convert c.txt and write to e.txt. Convert and replace a.txt. Convert and replace b.txt. Convert d.txt and write to f.txt. unix2dos -n c.txt e.txt -o a.txt b.txt -n d.txt f.txt DIAGNOSTICS
BUGS
The program does not work properly under MSDOS in stdio processing mode. If you know why is that so, please tell me. AUTHOR
Benjamin Lin - ( blin@socs.uts.edu.au ) MISCELLANY
Tested environment: Linux 1.2.0 with GNU C 2.5.8 SunOS 4.1.3 with GNU C 2.6.3 MS-DOS 6.20 with Borland C++ 4.02 Suggestions and bug reports are welcome. SEE ALSO
dos2unix(1) 1995.03.31 unix2dos v2.2 unix2dos(1)
All times are GMT -4. The time now is 10:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy