removing lines and characters while reading into an array - arrays

Trying to write a shell script that will read in a text file which looks something like:
Line A needs to be removed
Line B also to be removed
Line C which has lots of things, including characters that need removing should be the first to be read into an array position [0]
Line D
.
.
Line "n"
What I need to do is read from line C up to line n-1 into an array, but also remove the first 4 characters and the last 2 characters of the useful lines (Line C to Line n-1).
I can't seem to do anything other than read in the entire list, or print/echo the partial list but can't get that into an array.
I'm happy to multi-step it, rather than do it all in one line, but what ever is clean.

Try this if works
head -n-1 txt | awk '{if($0~/LineC/){i=1}; if(i>0){print substr($0,5,length($0)-2)}}' txt

Related

How to skip scanning a line when piping in input?

I am trying to pipe in input through stdin in the C programming language. I have a text file that I would like to read, however, the first line is just the header, so I need to somehow skip it and read the rest.
Example:
Name Grade Points Average
Lea A 4.0 3.3
...
I would like to place each line in a node as part of a linked list and so far all I have are the structures. I am unsure of how I am supposed to read the file.
For reference, what type in the command line will be the following:
./codename < input.txt
Thank you!
Use tail with -n:
tail -n+2 input.txt | codename
Alternatively, if you must read the file directly via the < operator, the easiest way is to call getline and throw out the output. You could also do something similar with fgets. You will have to modify the program directly to do that however.

Using awk to parse a string from a file

I'm still learning Unix and I'm having issues understanding the following line of code.
echo "$lines" | awk '{split($0,a,":"); print a[3],a[2],a[1]}'
I don't understand what is happening with the array a in the line of code above. Is it declaring the array and setting it equal to the string it's parsing? If it is declaring the array a, then, why can't I print out the results later on in the code?
echo "${a[1]}"
The line above prints an empty line and not what has been stored in the array a when the string was parsed. I know there is always something in the string that needs to be parsed and when I call the array a[1] I know that I'm in inside the scope. I just don't see/understand what is happening with the array a that prevents me from printing it out later on in the code.
Your code is printing a line for each line of input. If you dont have get output, my first guess would be, that you don't have input.
Given an input of:
lines="ab:cd:ef
ij:kl:m"
the output is:
ef cd ab
m kl ij
awk is executing the commands (which is everything in between the single quotes) for each line of input. First splitting the input line $0 at each : into an array a, then printing the first three elements in reverse order.
If you try to access an array element in the shell, what echo suggests, then you are too late. The array exists within awk and is gone when awk has finished.

C : Extracting sentences extended to next line

I have a file in which on each line there are multiple sentences separated by white spaces. SOmetimes one sentence may extend to next line. I want to extract these sentences separated by white space. My code successfully extracts sentences on same line separated by white space but since it is reading line by line. SO, issue comes when one sentence is extended to next line.
Store the part unused in creation of line at each iteration in temperary buffer. Include the buffer in the next iteration (append at the begining of line read).

Comparing a string from text file and then printing out the entire line(s)

My goal is to print out every full line from a text file if that line contains a string that is equivalent to user input.
I understand how to find the occurrences of a specific string in a text file, but I am confused as to how to associate that with a specific line. How do I relate my string with the specific line that it is in?
My initial thought was to store each line in an array and then print out that line if the user string is somewhere in that line.
However each line is a different size, so I was wondering if it is possible for me to initially divide my entire text file into x number of lines and then use a loop to go through each line and search for that string?
Save the file pointer of the starting of the line in a temp variable before starting new line compare

C - sorting a list on words in a text file

Wondering what method would be most suitable, and maybe, if someone would be so kind, how to start such a function for:
AWord
DWord
CWord
BWord
To end up in a new text file like:
AWord
BWord
CWord
DWord
At the moment, my program, reads each word line by line fgets() and does some stuff to it, capitalises the first letter etc. Once all that is done, a new text file is created, text.out.
I then want to sort it into alphabetical order, since all the words are only alphabetic, some maybe terminating with a number.
Thanks, any help is appreciated!
T.C
You said that you don't have an array of words, but that you read each line into a separate character array. This is what you need to fix. Declare char *lines[MAXLINES], an array of every line from your input file. Read each line from your input file in to that array. Then you can sort that array before putting it back out.
This is pretty much identical to the concepts presented in K&R Section 5.6 and 5.11.

Resources