Skip to contentSkip to conversation

split a file or return shell variable to a program

DISCUSSION vick Julius 6 participants 6 messages Apr 22, 2004 – Jul 14, 2004

lore
Share & tools
lore
#0 lore raw

Hello everbody
I have a text file I want to split. The file contains some sentences. between sentences I have empty 1 line. I want to split this file and put each sentence in a separate file with names 1, 2,3 ... do you have any idea to split it such as with awk or split?

Here is my strategy:

I wrote a C program in which I call bash shell script to increment a variable...

In the shell I defined the variable k such as:
$export k=1
in my bash script file,myFile, for testing, I put echo $k
let k+=1
(or this expression k=expr $k + 1)
echo $k

when I run this script file, it gives me
1
2

the problem is when I called form a C or C++ program, such system("echo $k");
//this gives 1
system("./myFile");
// this display
// 1
//2
system("echo $k");
//here the problem, it display 1 not 2

I want to have the incremented value for k, i.e 2 not the original one.

Do you have any hint?

Vick

_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*. http://join.msn.com/?page=features/featuredemail

#1 lore raw
Quoted 4 lines · vick Julius

I have a text file I want to split. The file contains some sentences. between sentences I have empty 1 line. I want to split this file and put each sentence in a separate file with names 1, 2,3 ...
do you have any idea to split it such as with awk or split?

You can't do it with split; that only handles the case where each contains a fixed number of bytes or lines.

You can do it with awk easily enough, e.g.:

	#!/usr/bin/awk -f
	BEGIN {
		ofile = 0;
	}
	
	/^ *$/ {
		close(ofile);
		ofile++;
	}
	
	/[^ ]/ {
		print > ofile;
	}
Quoted 27 lines

Here is my strategy:

I wrote a C program in which I call bash shell script to increment a variable...

In the shell I defined the variable k such as:
$export k=1
in my bash script file,myFile, for testing, I put echo $k
let k+=1
(or this expression k=expr $k + 1)
echo $k

when I run this script file, it gives me
1
2

the problem is when I called form a C or C++ program, such system("echo $k");
//this gives 1
system("./myFile");
// this display
// 1
//2
system("echo $k");
//here the problem, it display 1 not 2

Quoted 1 line

I want to have the incremented value for k, i.e 2 not the original one.

Each process has its own set of environment variables. A process can modify its own environment variables, but not those of another process.

If you want to share state between processes, use a file.

Signature · 1 line
Glynn Clements <[email protected]>
#3 lore raw
In reply to vick Julius

man setenv.

But anyway you could just:

perl -ne '/\S/ and $x++, echo "$_">>out.$x' < your_file

Quoted 54 lines · vick Julius

Date: Thu, 22 Apr 2004 18:33:45 +0000
From: vick Julius <[email protected]>
To: [email protected]
Subject: split a file or return shell variable to a program

Hello everbody
I have a text file I want to split. The file contains some sentences. between sentences I have empty 1 line. I want to split this file and put each sentence in a separate file with names 1, 2,3 ...
do you have any idea to split it such as with awk or split?

Here is my strategy:

I wrote a C program in which I call bash shell script to increment a variable...

In the shell I defined the variable k such as:
$export k=1
in my bash script file,myFile, for testing, I put echo $k
let k+=1
(or this expression k=expr $k + 1)
echo $k

when I run this script file, it gives me
1
2

the problem is when I called form a C or C++ program, such system("echo $k");
//this gives 1
system("./myFile");
// this display
// 1
//2
system("echo $k");
//here the problem, it display 1 not 2

I want to have the incremented value for k, i.e 2 not the original one.

Do you have any hint?

Vick

_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
In reply to Glynn Clements

here's another way to do it (just with bash):

counter=1
while read line
do
if [ -z $line ] ; then continue
else
echo $line > $counter
counter=expr $counter + 1
fi
done < your_sentences_file

Quoted 76 lines · Glynn Clements
Quoted 1 line · vick Julius

I have a text file I want to split. The file contains some

sentences.

Quoted 1 line

between sentences I have empty 1 line. I want to split this file and

put

Quoted 2 lines

each sentence in a separate file with names 1, 2,3 ... do you have any idea to split it such as with awk or split?

You can't do it with split; that only handles the case where each contains a fixed number of bytes or lines.

You can do it with awk easily enough, e.g.:

	#!/usr/bin/awk -f
	BEGIN {
		ofile = 0;
	}
	 
	/^ *$/ {
		close(ofile);
		ofile++;
	}
	 
	/[^ ]/ {
		print > ofile;
	}
Quoted 28 lines

Here is my strategy:

I wrote a C program in which I call bash shell script to increment a

variable...

In the shell I defined the variable k such as:
$export k=1
in my bash script file,myFile, for testing, I put echo $k
let k+=1
(or this expression k=expr $k + 1)
echo $k

when I run this script file, it gives me
1
2

the problem is when I called form a C or C++ program, such system("echo $k");
//this gives 1
system("./myFile");
// this display
// 1
//2
system("echo $k");
//here the problem, it display 1 not 2

Quoted 1 line

I want to have the incremented value for k, i.e 2 not the original

one.

Each process has its own set of environment variables. A process can modify its own environment variables, but not those of another process.

If you want to share state between processes, use a file.

--
Glynn Clements <[email protected]>
-
To unsubscribe from this list: send the line "unsubscribe linux- admin"
in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw

running fsck in runlevel 3 and single user mode serial access problems

In reply to [email protected]

Is there a safe way to run fsck in run level 3?
I have a situation where if I shutdown this system it will not boot until I run fsck to clean up the disk. Also, it is a headless Linux system and if I try to access the system in single user mode over the serial port, I keep getting a new maintenance prompt after every keystroke. So I cannot get into single user mode from the seiral port and I cannot complete my boot
until I run fsck from single user mode. Bummer.

I was able to attach a monitor and keyboard and enter single user mode from the console. Then I can run fsck and finish my boot. But i do not want to keep physically attaching a keyboard and monitor to this machine and trottting over to it every time it needs a reboot.

Any thoughts on how to handle this?

Thanks,
Rudy

#5 lore raw

Re: running fsck in runlevel 3 and single user mode serial access problems

Quoted 1 line · you

Is there a safe way to run fsck in run level 3?

The only important thing is, that the partition on which you want to run is mounted read-only.

So you can try to remount it:

mount -o remount,ro /mountpoint

Finished fsck you can mount it writable with

mount -o remount,rw /mountpoint

Hope this helps

Lothar