LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   join files using shell script (https://www.linuxquestions.org/questions/linux-software-2/join-files-using-shell-script-468455/)

sailu_mvn 07-28-2006 01:40 AM

join files using shell script
 
I have 3 files that are to be joined. Can anybody tell me how to stat writing a shell script?

And one more thing. In a file if at some point if i need to insert some text, how to do it.

For eg, a file called a.txt has at somepoint "NOTHING CAN HAPPEN" line. After that if i want to insert a paragraph, can i do that?

crazyjimbo 07-28-2006 01:43 AM

To join files, use:

Code:

cat inputfile1 inputfile2 inputfile3 > outputfile

sailu_mvn 07-28-2006 01:51 AM

yeah i know it
if i have a file and i have to insert a paragraph at some articular localtion say line number 45
How to do it

spirit receiver 07-28-2006 02:02 AM

This will make a backup of the original file:
Code:

sed -i.bak -e'45,s/^/your new text here\n/' your_filename

raskin 07-28-2006 02:05 AM

line_number=$(cat f1 |wc -l);
head -45 f1; cat f2; tail -$((line_number-45)) f1;

konsolebox 07-28-2006 02:13 AM

well we meet again raskin.

to sailu_mvn you can try my script

Code:

#!/bin/bash

FILE=
INJECT=
LINENO=

{
        cat "$FILE" | sed -n 1,$((LINENO - 1))p
        cat "$INJECT"
        cat "$FILE" | sed -n ${LINENO},\$p
} > .tmp

cat .tmp > "$FILE"
rm .tmp

if something's wrong just try to fix it. it's easy to get its idea.

regards :)


All times are GMT -5. The time now is 03:45 PM.