Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Add string to middle of a file Post 302076583 by Unbeliever on Wednesday 14th of June 2006 10:25:04 AM
Old 06-14-2006
Example working code:
Code:
#!/bin/sh
text="Text to add"
file=data.txt

lines=`wc -l $file | awk '{print $1}'`

middle=`expr $lines / 2`

# If the file has an odd number of lines this script adds the text
# after the middle line. Comment this block out to add before
if [ `expr $lines % 2` -eq 1 ]
then
  middle=`expr $middle + 1`
fi

sed -e "${middle}a $text" $file

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add Lines in middle of file

I need to add a new lines with certain data, to an existing file, every 5 lines in the file. There is no way to find any distinct charater pattern so I will have to do a line count and then insert the new line. I think using awk or sed is what I need to do. Any help is appreciated. Kunder (2 Replies)
Discussion started by: kunder
2 Replies

2. Shell Programming and Scripting

Problem to add the string(without sed & awk) into the middle of file

Hi, I have tried many times to add the string into the first line of the file or the middle of the file but could not find the solution. I first tried by $echo "paki" >> file This code only append paki string at the end of file "file" but how can i add this "paki" into the first line or... (5 Replies)
Discussion started by: ali hussain
5 Replies

3. Shell Programming and Scripting

add a string in the middle of the file

i want to add a string in a very top of a file without using VI or SED or AWK this is what ive done: (echo '0a'; echo 'LINE OF TEXT'; echo '.'; echo 'wq') | ed -s myfile to add astrng right in the middle i could have count the lines of the file and just chenge the address. ... (6 Replies)
Discussion started by: ciroredz
6 Replies

4. Shell Programming and Scripting

add text in the middle of file

Can anyone help me pls? I want to add a text into the middle of file. I've writtenthe following script text to add="$1" file="$2" lines=$(wc -l $2) half_lines=$(expr $lines / 2) head -$half_lines $2 > temp echo "text to add" >> temp ((half_lines=$half_lines + 1)) tail -$half_lines $2... (6 Replies)
Discussion started by: relle
6 Replies

5. Shell Programming and Scripting

Script to add a single line to middle of text file.

I've got a configuration file that is filled with xml text statements for example: <...../> <...../> <...../> <data id="java-options" value="-server -Djava.security.policy..../> <...../> <...../> <...../> I want to write a korn shell script that will go to this specific line and add a... (2 Replies)
Discussion started by: progkcp
2 Replies

6. Shell Programming and Scripting

stripping certain characters in at the middle of a string

I am trying to strip out certain characters from a string on both (left & right) sides. For example, line=see@hear|touch, i only want to echo the "hear" part. Well i have tried this approach: line=see@hear|touch templine=${line#*@} #removed "see@" echo ${templine%%\|*} #removed... (4 Replies)
Discussion started by: mcoblefias
4 Replies

7. UNIX for Dummies Questions & Answers

Splitting a string and putting another string in the middle?

Hello all! I'm trying to put together a small script that will take in a file name and attach a datestamp to the end of it (but before the file type extension). To illustrate... Before: filename.txt anotherfilename.txt After: filename_20090724.txt anotherfilename_20090724.txt ... (7 Replies)
Discussion started by: jisoo411
7 Replies

8. Linux

Remove newline in middle of string

my file input is with tab as delimiter, and in every line, there would be a skip of line with an unexcepted newline breaker. I'd like to remove this \n and put the information in the same line. INPUT a1 b1b2 c1 c2 d1 a2 b3 c3 d4 OUTPUT a1 b1b2 c1c2 ... (9 Replies)
Discussion started by: kinkichin
9 Replies

9. UNIX for Dummies Questions & Answers

Map a string in a the middle of a record

Hi Folks! I hope you can help me figure this out. I need to print a record which contains the contents of a config file. The contents of the config file should be found within the 21st and the 30th position of the fixed width reference file. Config File: aaaa bbbb cccc Reference... (3 Replies)
Discussion started by: kokoro
3 Replies

10. UNIX for Beginners Questions & Answers

Views How to replace a CRLF char from a variable length file in the middle of a string in UNIX?

My sample file is variable length, with out any field delimiters. It has min of 18 chars length and the 'CRLF' is potentially between 12-14 chars. How do I replace this with a space? I still want to keep end of record, but just want to remove these new lines chars in the middle of the data. ... (7 Replies)
Discussion started by: chandrath
7 Replies
Signature(3pm)						User Contributed Perl Documentation					    Signature(3pm)

NAME
perl5i::Signature - Representing what parameters a subroutine accepts SYNOPSIS
func hello( $greeting, $place ) { say "$greeting, $place" } my $code = &hello; my $signature = $code->signature; say $signature->num_positional_params; # 2 say $signature->is_method; # false DESCRIPTION
A Signature is a representation of what parameters a subroutine accepts. Each subroutine defined with "func" or "method" will have a signature associated with it. You can get at it by calling the "signature" method on the code reference. See "Signature Introspection" in perl5i for more details. Subroutines declared with Perl's built in "sub" will have no signature. METHODS
params my $params = $sig->params; An array ref of the parameters a subroutine takes in the order it takes them. Currently they are just strings. In the future they will be string overloaded objects. positional_params my $params = $sig->positional_params; Like "$sig->params" but it is just the positional parameters. In the future there will be named parameters. num_positional_params my $num_positional_params = $sig->num_positional_params; The number of named parameters the subroutine takes. In the future there will be named parameters. For the purposes of determining how many arguments a function takes, it is most useful to look just at the positional ones. This is mostly an optimization for "$sig->positional_params->size". as_string my $params = $sig->as_string; The original signature string. invocant my $invocant = $sig->invocant; The invocant is the object or class a method is called on. "invocant" will return the parameter which contains this, by default it is $self on a method, and nothing a regular subroutine. is_method my $is_method = $sig->is_method; Returns if the subroutine was declared as a method. OVERLOADING
Signature objects are string overloaded to return "as_string". They are also always true to avoid objects taking no parameters from being confused with subroutines with no signatures. perl v5.14.2 2012-06-14 Signature(3pm)
All times are GMT -4. The time now is 01:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy