Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Shell - Read a text file with two words and extract data Post 302983628 by Scrutinizer on Friday 14th of October 2016 04:23:25 AM
Old 10-14-2016
You're welcome,

-a means read into an array (when using bash). F is the name of the array

printf is the preferred and standardized alternative to echo. The first field to printf is the "format string" . "%s" means "string" and "\n" means new line". See: printf or the bash man page.

To get all the element of the array one normally uses: "${F[@]}"
"${F[@]# }" does the same, but in addition it uses parameter expansion and # means cut off a leading space if it exists.

Since F is an array it will work on every element of the array (more about this in the bash man page).

With the while read loop, for every line of the input file, the array F gets filled anew.

Last edited by Scrutinizer; 10-14-2016 at 05:34 AM..
This User Gave Thanks to Scrutinizer For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to read all the unique words in a text file

How can i read all the unique words in a file, i used - cat comment_file.txt | /usr/xpg6/bin/tr -sc 'A-Za-z' '/012' and cat comment_file.txt | /usr/xpg6/bin/tr -sdc 'A-Za-z' '/012' but they didnt worked..... (5 Replies)
Discussion started by: aditya.ece1985
5 Replies

2. Shell Programming and Scripting

To read and separate number and words in file and store to two new file using shell

hi, I am a begginer in unix and i want to know how to open a file and read it and separate the numbers & words and storing it in separate files, Using shell scripting. Please help me out for this. Regards S.Kamakshi (2 Replies)
Discussion started by: kamakshi s
2 Replies

3. Shell Programming and Scripting

shell script to read data from text file and to load it into a table in TOAD

Hi....can you guys help me out in this script?? Below is a portion text file and it contains these: GEF001 000093625 MKL002510 000001 000000 000000 000000 000000 000000 000001 GEF001 000093625 MKL003604 000001 000000 000000 000000 000000 000000 000001 GEF001 000093625 MKL005675 000001... (1 Reply)
Discussion started by: pallavishetty
1 Replies

4. Shell Programming and Scripting

Shell script to read lines in a text file and filter user data

hi all, I have this file with some user data. example: $cat myfile.txt FName|LName|Gender|Company|Branch|Bday|Salary|Age aaaa|bbbb|male|cccc|dddd|19900814|15000|20| eeee|asdg|male|gggg|ksgu|19911216||| aara|bdbm|male|kkkk|acke|19931018||23| asad|kfjg|male|kkkc|gkgg|19921213|14000|24|... (4 Replies)
Discussion started by: srimal
4 Replies

5. Shell Programming and Scripting

How to read the data from the text file in shell script?

I am having one text file and i need to read that data from my shell script. I will expain you the scenario: Script look like: For name type 1: For age type 2: For Salary type3: echo "Enter the input:" read the data if input is 1 then go to the Text file and print the... (2 Replies)
Discussion started by: dineshmurs
2 Replies

6. Shell Programming and Scripting

Script to read file and extract data by matching pattern

Hello, I have a file ( say file1) which has lines like below. xxxx:xxxx,yyyy,1234,efgh zzzz:zzzz,kkkk,pppp,1234,xxxx,uuuu,oooo dddd:dddd here the word before ":" ( ie: xxxx) is the file name and the string after : are also file names, but each file name separated by "," In case of... (20 Replies)
Discussion started by: pradeepmacha
20 Replies

7. Shell Programming and Scripting

regular expression with shell script to extract data out of a text file

hi i am trying to extract some specific data out of a text file using regular expressions with shell script that is using a multiline grep .. and the tool i am using is pcregrep so that i can get compatibility with perl's regular expressions for a sample data like this, i am trying to grab... (6 Replies)
Discussion started by: vemkiran
6 Replies

8. Shell Programming and Scripting

I need to extract uique words from text file

Hello programmers, I need to create a list of unique words from a text file using PERL...may i have the code for that please? Thank you (1 Reply)
Discussion started by: alsohari
1 Replies

9. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

10. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies
LSEARCH(3)						   BSD Library Functions Manual 						LSEARCH(3)

NAME
lsearch, lfind -- linear search and append LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <search.h> void * lsearch(const void *key, void *base, size_t *nelp, size_t width, int (*compar)(const void *, const void *)); void * lfind(const void *key, const void *base, size_t *nelp, size_t width, int (*compar)(const void *, const void *)); DESCRIPTION
The lsearch() and lfind() functions walk linearly through an array and compare each element with the one to be sought using a supplied com- parison function. The key argument points to an element that matches the one that is searched. The array's address in memory is denoted by the base argument. The width of one element (i.e., the size as returned by sizeof()) is passed as the width argument. The number of valid elements contained in the array (not the number of elements the array has space reserved for) is given in the integer pointed to by nelp. The compar argument points to a function which compares its two arguments and returns zero if they are matching, and non-zero otherwise. If no matching element was found in the array, lsearch() copies key into the position after the last element and increments the integer pointed to by nelp. RETURN VALUES
The lsearch() and lfind() functions return a pointer to the first element found. If no element was found, lsearch() returns a pointer to the newly added element, whereas lfind() returns NULL. Both functions return NULL if an error occurs. EXAMPLES
#include <search.h> #include <stdio.h> #include <stdlib.h> static int element_compare(const void *p1, const void *p2) { int left = *(const int *)p1; int right = *(const int *)p2; return (left - right); } int main(int argc, char **argv) { const int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; size_t element_size = sizeof(array[0]); size_t array_size = sizeof(array) / element_size; int key; void *element; printf("Enter a number: "); if (scanf("%d", &key) != 1) { printf("Bad input0); return (EXIT_FAILURE); } element = lfind(&key, array, &array_size, element_size, element_compare); if (element != NULL) printf("Element found: %d0, *(int *)element); else printf("Element not found0); return (EXIT_SUCCESS); } SEE ALSO
bsearch(3), hsearch(3), tsearch(3) STANDARDS
The lsearch() and lfind() functions conform to IEEE Std 1003.1-2001 (``POSIX.1''). HISTORY
The lsearch() and lfind() functions appeared in 4.2BSD. In FreeBSD 5.0, they reappeared conforming to IEEE Std 1003.1-2001 (``POSIX.1''). BSD
April 21, 2013 BSD
All times are GMT -4. The time now is 12:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy