Sponsored Content
Full Discussion: Change value in a textFile
Top Forums Shell Programming and Scripting Change value in a textFile Post 302765191 by Xedrox on Friday 1st of February 2013 03:51:21 PM
Old 02-01-2013
Change value in a textFile

Hi everyone,

I need to make a script to take three parameters:

-> KEY
-> NEW_VALUE
-> FILE

The FILE is a text plane file.
The KEY is a variable to configure, for example:

Code:
KEY1   =    HOLA
KEY2=   HOLA
KEY3=HELLO
KEY4           =HOLA

And the NEW_VALUE is the string next to symbol "=".

The script must change a value of a "KEY". For example, supose i have the next file:

Code:
KEY1   =    HOLA
KEY2   =   HOLA
KEY3   =HELLO
KEY4           =HOLA

I execute the next comand: ./script 'KEY2' 'BAZINGA' 'myFile.txt'

The result must be:

Code:
KEY1   =    HOLA
KEY2   =BAZINGA
KEY3   =HELLO
KEY4           =HOLA

I did the next script:

Code:
CLAVE=$1
NEWVALUE=$2
ARCHIVO_DESTINO=$3

if [ ! -f $ARCHIVO_DESTINO ]
then
	echo "ERROR EN CHANGE_VALUE_CONFIG, NO se encontro el archivo: $ARCHIVO_DESTINO"
	exit 1
fi


awk -F= -v P1="$CLAVE" -v P2="$NEWVALUE" '$1==P1{$0=P2}1' $ARCHIVO_DESTINO > temporalConfig

rm $ARCHIVO_DESTINO
mv temporalConfig $ARCHIVO_DESTINO

The problem is: just works well, when there is not space between "KEY" and "=" and "VALUE". For example "KEY1=HOLA". I need to this work with the next format "KEY1 =HOLA". How can i change my script?

Thanks!
Sorry for my english!
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

plugging out value from a textfile

Hi, need some help from all of you as i'm a beginner in shellscript. Currently i have a textfile(filename.txt) with the below content: TOTAL: 30 RECORDS: 300 anyone one know how do i plug out the value of 30 and put into a variable(var1) and 300 into another variable(var2)?I'm coding using... (7 Replies)
Discussion started by: snowfrost
7 Replies

2. Shell Programming and Scripting

manipulate the textfile having strings

i have text files named like vho1.txt, vho2.txt... vho15.txt.In all there are 15 in number. each file has strings like \Movies On Demand\Action & Adventure|X-Men: Wolverine|2009-09-29 00:01:00|2010-03-24 23:59:00|Active|3 \Movies On Demand\Free\Free1|My movie 14|2010-01-11 00:00:00|2010-03-01... (5 Replies)
Discussion started by: ramse8pc
5 Replies

3. Shell Programming and Scripting

Textfile lesson

Tag allerseits Ich habe ein umfangreiches Script. Darin möchte ich zu Beginn ein textfile lesen. Den ersten Satz. Dann kommen mehrere Instruktionen und dann soll wieder gelesen werden. Den zweiten Satz. Etc. Ich kann also das herkömmliche while read xyz / do ... done nicht benützen. ... (0 Replies)
Discussion started by: lazybaer
0 Replies

4. Shell Programming and Scripting

How to extract more than 1 line in a textfile ?

Hi, I'm starting a little project with a shell script but I'm don't know how to do it. Maybe someone can help me. I have un text file like this : I'd like to do a script who will extract from my file from @ADDLINE1@ to @ADDLINE4@ only and I have no idea how to do this. Any idea ? ... (7 Replies)
Discussion started by: Poulki
7 Replies

5. Shell Programming and Scripting

Cut lines from and to in a textfile

i am having a text file like below rama surya pandu latha singh raja i want to get the new file from 3 to 5 i.e pandu latha singh please help (1 Reply)
Discussion started by: suryanarayana
1 Replies

6. Windows & DOS: Issues & Discussions

Use Textfile for variables ?

So... I have a text file that contains this (hex.txt): #8C7CA6 #6C70A5 #75777C #959A90 #7A7C6C #867DAB #80867E #8A87BD #6B71C6 #8F8A79 #9A9DCE #7E87D0 #69709E #82968C #7C8F81 #A3917B (5 Replies)
Discussion started by: pasc
5 Replies

7. Shell Programming and Scripting

How to separate sorte different characters from one textfile and copy them in a new textfile?

My first post, so don't kill me :) Say i open some textfile with some example like this. on the table are handy, bread and wine Now i know exactly what is in and i want to separate and sorted it in terminal to an existing file with another 2 existing lines in like this: table plane ... (3 Replies)
Discussion started by: schwatter
3 Replies

8. Shell Programming and Scripting

Replicating certain lines in a textfile

I am very new to to shell scripting and facing a problem that I can't seem to solve. I want to write a bash script that edits file1.txt and saves it as file2.txt. This is what the files should look like: file1: textline1 textline2 startCopy copyThis endCopy textline3 textline4 file2: ... (6 Replies)
Discussion started by: sandy90
6 Replies

9. Shell Programming and Scripting

Variablecontent in a Textfile

I want to save a variablecontent in a Textfile. How can i do that? These works only with ls shell_exec("ls > text.txt");Please use code tags, thanks (2 Replies)
Discussion started by: Linuxmann
2 Replies
PTHREAD_CREATE(3)					     Linux Programmer's Manual						 PTHREAD_CREATE(3)

NAME
pthread_create - create a new thread SYNOPSIS
#include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); Compile and link with -pthread. DESCRIPTION
The pthread_create() function starts a new thread in the calling process. The new thread starts execution by invoking start_routine(); arg is passed as the sole argument of start_routine(). The new thread terminates in one of the following ways: * It calls pthread_exit(3), specifying an exit status value that is available to another thread in the same process that calls pthread_join(3). * It returns from start_routine(). This is equivalent to calling pthread_exit(3) with the value supplied in the return statement. * It is canceled (see pthread_cancel(3)). * Any of the threads in the process calls exit(3), or the main thread performs a return from main(). This causes the termination of all threads in the process. The attr argument points to a pthread_attr_t structure whose contents are used at thread creation time to determine attributes for the new thread; this structure is initialized using pthread_attr_init(3) and related functions. If attr is NULL, then the thread is created with default attributes. Before returning, a successful call to pthread_create() stores the ID of the new thread in the buffer pointed to by thread; this identifier is used to refer to the thread in subsequent calls to other pthreads functions. The new thread inherits a copy of the creating thread's signal mask (pthread_sigmask(3)). The set of pending signals for the new thread is empty (sigpending(2)). The new thread does not inherit the creating thread's alternate signal stack (sigaltstack(2)). The new thread inherits the calling thread's floating-point environment (fenv(3)). The initial value of the new thread's CPU-time clock is 0 (see pthread_getcpuclockid(3)). Linux-specific details The new thread inherits copies of the calling thread's capability sets (see capabilities(7)) and CPU affinity mask (see sched_setaffin- ity(2)). RETURN VALUE
On success, pthread_create() returns 0; on error, it returns an error number, and the contents of *thread are undefined. ERRORS
EAGAIN Insufficient resources to create another thread, or a system-imposed limit on the number of threads was encountered. The latter case may occur in two ways: the RLIMIT_NPROC soft resource limit (set via setrlimit(2)), which limits the number of process for a real user ID, was reached; or the kernel's system-wide limit on the number of threads, /proc/sys/kernel/threads-max, was reached. EINVAL Invalid settings in attr. EPERM No permission to set the scheduling policy and parameters specified in attr. CONFORMING TO
POSIX.1-2001. NOTES
See pthread_self(3) for further information on the thread ID returned in *thread by pthread_create(). Unless real-time scheduling policies are being employed, after a call to pthread_create(), it is indeterminate which thread--the caller or the new thread--will next execute. A thread may either be joinable or detached. If a thread is joinable, then another thread can call pthread_join(3) to wait for the thread to terminate and fetch its exit status. Only when a terminated joinable thread has been joined are the last of its resources released back to the system. When a detached thread terminates, its resources are automatically released back to the system: it is not possible to join with the thread in order to obtain its exit status. Making a thread detached is useful for some types of daemon threads whose exit status the application does not need to care about. By default, a new thread is created in a joinable state, unless attr was set to create the thread in a detached state (using pthread_attr_setdetachstate(3)). On Linux/x86-32, the default stack size for a new thread is 2 megabytes. Under the NPTL threading implementation, if the RLIMIT_STACK soft resource limit at the time the program started has any value other than "unlimited", then it determines the default stack size of new threads. Using pthread_attr_setstacksize(3), the stack size attribute can be explicitly set in the attr argument used to create a thread, in order to obtain a stack size other than the default. EXAMPLE
The program below demonstrates the use of pthread_create(), as well as a number of other functions in the pthreads API. In the following run, on a system providing the NPTL threading implementation, the stack size defaults to the value given by the "stack size" resource limit: $ ulimit -s 8192 # The stack size limit is 8 MB (0x80000 bytes) $ ./a.out hola salut servus Thread 1: top of stack near 0xb7dd03b8; argv_string=hola Thread 2: top of stack near 0xb75cf3b8; argv_string=salut Thread 3: top of stack near 0xb6dce3b8; argv_string=servus Joined with thread 1; returned value was HOLA Joined with thread 2; returned value was SALUT Joined with thread 3; returned value was SERVUS In the next run, the program explicitly sets a stack size of 1MB (using pthread_attr_setstacksize(3)) for the created threads: $ ./a.out -s 0x100000 hola salut servus Thread 1: top of stack near 0xb7d723b8; argv_string=hola Thread 2: top of stack near 0xb7c713b8; argv_string=salut Thread 3: top of stack near 0xb7b703b8; argv_string=servus Joined with thread 1; returned value was HOLA Joined with thread 2; returned value was SALUT Joined with thread 3; returned value was SERVUS Program source #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <ctype.h> #define handle_error_en(en, msg) do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0) struct thread_info { /* Used as argument to thread_start() */ pthread_t thread_id; /* ID returned by pthread_create() */ int thread_num; /* Application-defined thread # */ char *argv_string; /* From command-line argument */ }; /* Thread start function: display address near top of our stack, and return upper-cased copy of argv_string */ static void * thread_start(void *arg) { struct thread_info *tinfo = (struct thread_info *) arg; char *uargv, *p; printf("Thread %d: top of stack near %p; argv_string=%s ", tinfo->thread_num, &p, tinfo->argv_string); uargv = strdup(tinfo->argv_string); if (uargv == NULL) handle_error("strdup"); for (p = uargv; *p != ''; p++) *p = toupper(*p); return uargv; } int main(int argc, char *argv[]) { int s, tnum, opt, num_threads; struct thread_info *tinfo; pthread_attr_t attr; int stack_size; void *res; /* The "-s" option specifies a stack size for our threads */ stack_size = -1; while ((opt = getopt(argc, argv, "s:")) != -1) { switch (opt) { case 's': stack_size = strtoul(optarg, NULL, 0); break; default: fprintf(stderr, "Usage: %s [-s stack-size] arg... ", argv[0]); exit(EXIT_FAILURE); } } num_threads = argc - optind; /* Initialize thread creation attributes */ s = pthread_attr_init(&attr); if (s != 0) handle_error_en(s, "pthread_attr_init"); if (stack_size > 0) { s = pthread_attr_setstacksize(&attr, stack_size); if (s != 0) handle_error_en(s, "pthread_attr_setstacksize"); } /* Allocate memory for pthread_create() arguments */ tinfo = calloc(num_threads, sizeof(struct thread_info)); if (tinfo == NULL) handle_error("calloc"); /* Create one thread for each command-line argument */ for (tnum = 0; tnum < num_threads; tnum++) { tinfo[tnum].thread_num = tnum + 1; tinfo[tnum].argv_string = argv[optind + tnum]; /* The pthread_create() call stores the thread ID into corresponding element of tinfo[] */ s = pthread_create(&tinfo[tnum].thread_id, &attr, &thread_start, &tinfo[tnum]); if (s != 0) handle_error_en(s, "pthread_create"); } /* Destroy the thread attributes object, since it is no longer needed */ s = pthread_attr_destroy(&attr); if (s != 0) handle_error_en(s, "pthread_attr_destroy"); /* Now join with each thread, and display its returned value */ for (tnum = 0; tnum < num_threads; tnum++) { s = pthread_join(tinfo[tnum].thread_id, &res); if (s != 0) handle_error_en(s, "pthread_join"); printf("Joined with thread %d; returned value was %s ", tinfo[tnum].thread_num, (char *) res); free(res); /* Free memory allocated by thread */ } free(tinfo); exit(EXIT_SUCCESS); } BUGS
In the obsolete LinuxThreads implementation, each of the threads in a process has a different process ID. This is in violation of the POSIX threads specification, and is the source of many other nonconformances to the standard; see pthreads(7). SEE ALSO
getrlimit(2), pthread_attr_init(3), pthread_cancel(3), pthread_detach(3), pthread_equal(3), pthread_exit(3), pthread_getattr_np(3), pthread_join(3), pthread_self(3), pthreads(7) COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2008-11-11 PTHREAD_CREATE(3)
All times are GMT -4. The time now is 07:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy