logo
down
shadow

Linux printing alternate 2 lines in text file


Linux printing alternate 2 lines in text file

Content Index :

Linux printing alternate 2 lines in text file
Tag : bash , By : itsmegb
Date : December 05 2020, 12:18 PM

it should still fix some issue I've a large text file with the following lines: , you can use this (awk)
 cat test.txt | awk '{if (((NR % 4)==1) || ((NR % 4)==2)) {print}}'

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

printing last 5 lines of file in C in Linux


Tag : c , By : msugar
Date : March 29 2020, 07:55 AM
I hope this helps you . Why not count the number of characters read while reading back to the fifth newline (call that n) and then do a read of n characters? You don't need to store the data, it's already stored in the file.

C++- Code not working to alternate lines when writing on a text file


Tag : cpp , By : Adam May
Date : March 29 2020, 07:55 AM
wish of those help The code looks correct and should work under normal circumstances. This might be a problem with the encoding of the second file, where the newline characters are not being recognised as such on your platform, which could result in the entire second file being interpreted as a single line by the C++ standard library.
Windows (CR+LF), Unix/Linux (LF), and Mac (CR) each have different conventions for newlines. Search about the carriage return and line feed characters across platforms to learn more about this topic.

how to write alternate lines in text file to another text file in python?


Tag : python , By : usingtechnology
Date : March 29 2020, 07:55 AM
like below fixes the issue First of all,for safety reasons, you must open your file using the with keyword instead. Even you're not working on a a more important project with your actual code, it is good to have such good programming habits.
I propose you a simple approach to resolve your problem:
# Safely open your file
with open('file.txt', 'r') as f:
   lines = f.readlines()

# Rearrange the lines according to the logic you are looking for
even_lines, odd_lines = lines[::2], lines[1::2]

# Safely open your files
with open('file1.txt','w') as f1:
   for odd_line in odd_lines:
       f1.write(odd_line)

with open('file2.txt','w') as f2:
   for even_line in even_lines:
       f2.write(even_line)
secuirty@begueradj:~/Desktop$ python tester.py 
security@begueradj:~/Desktop$ cat file1.txt
This is the line number 1
This is the line number 3
This is the line number 5
security@begueradj:~/Desktop$ cat file2.txt
This is the line number 0
This is the line number 2
This is the line number 4
hacker@begueradj:~/Desktop$ 

How to Start and Stop printing lines with Specific Text in Linux/Unix Terminal


Tag : linux , By : Philofax
Date : March 29 2020, 07:55 AM
I wish this help you A simple way in awk is just keep a flag and set n = 1 when "Start" is encountered in field 4. Checking n == 1 gives you a test to print each line while n is 1. When "End" is reached, simply print that line and exit, e.g.
awk '$4 == "End" {print; exit} $4 == "Start" {n = 1} n == 1' file
$ awk '$4 == "End" {print; exit} $4 == "Start" {n = 1} n == 1' file
2019/10/17 23:01:02 -I- Start
2019/10/17 23:01:02 -I- ======================
2019/10/17 23:05:02 -I- Summary Details
2019/10/17 23:10:02 -I- End
awk '$4 == "Start" {n = 1} n == 1; $4 == "End" {exit}' file
awk -v date="$(date +%Y/%m/%d)" '
    $1 == date { 
        if($4 == "Start") n = 1
        if(n == 1) {print}
        if($4 == "End") exit
    }
' file
$ cat file
2019/10/17 23:01:02 -W- Blah Blah Blah.....
2019/10/17 23:01:02 -I- Blah Blah Blah.....
2019/10/17 23:01:02 -I- Blah Blah Blah.....
2019/10/17 23:01:02 -I- Start
2019/10/17 23:01:02 -I- ======================
2019/10/17 23:05:02 -I- Summary Details
2019/10/17 23:10:02 -I- End
2019/10/17 23:10:02 -I- Blah Blah Blah.....
2019/10/17 23:10:02 -I- Blah Blah Blah.....
2019/10/17 23:10:02 -I- Blah Blah Blah.....
2019/10/20 23:01:02 -W- Blah Blah Blah.....
2019/10/20 23:01:02 -I- Blah Blah Blah.....
2019/10/20 23:01:02 -I- Blah Blah Blah.....
2019/10/20 23:01:02 -I- Start
2019/10/20 23:01:02 -I- ======================
2019/10/20 23:05:02 -I- Summary Details
2019/10/20 23:10:02 -I- End
2019/10/20 23:10:02 -I- Blah Blah Blah.....
2019/10/20 23:10:02 -I- Blah Blah Blah.....
2019/10/20 23:10:02 -I- Blah Blah Blah.....
$ awk -v date="$(date +%Y/%m/%d)" '
>     $1 == date {
>         if($4 == "Start") n = 1
>         if(n == 1) {print}
>         if($4 == "End") exit
>     }
> ' file
2019/10/20 23:01:02 -I- Start
2019/10/20 23:01:02 -I- ======================
2019/10/20 23:05:02 -I- Summary Details
2019/10/20 23:10:02 -I- End

Printing alternate lines using python


Tag : python , By : Guid
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • How to determine if an EXIT trap is defined for a BASH subshell?
  • How can you tokenize a bash string into an array when the string includes a * character?
  • Use OPTARG as variable in shell script, no matter the order of arguments
  • Why does my loop occasionally not read the entire line?
  • Is there any shorter syntax for changing several directory levels back up with bash cd?
  • Correctly formatting a PYTHONPATH in my .bash_profile
  • Bash Script : Check if number starts with
  • im getting a syntax error when i run my shell script, the error is in my for each loop and says unespected token `;;'
  • Write a shell script that will return the list of files skipped by your repository git
  • Create a shell script that returns the last 5 hash of my commits of my git deposit
  • How to copy Associative Array without for loop
  • Extract specific string from line with standard grep,egrep or awk
  • Copy files that have at least the mention of one certain word
  • How it exactly works?
  • How to insert a specific character at a specific line of a file using sed or awk?
  • How to create bash script that combines specified list of items at random?
  • How to get the return code from the bash command
  • Running a bash find with file cp parameter error python script
  • How to pass a file as an argument to a bash script function?
  • How can I pass a complex file glob to a subshell?
  • Standard-in parse errors with bc command
  • Using AWK to merge unique rows based on column one
  • Bash Login Script Stalls
  • BASH replace a single letter only when it's alone not in a word
  • Compare 4 files line by line to see if they match or don't match
  • Add character to file name if duplicate when moving with bash
  • Use grep only on specific columns in many files?
  • xor conditional in bash
  • Fails to create azure container right after storage account was created
  • bash: arithmetic expressions inside of variables
  • jq command throws error "cannot iterate over string" when
  • In Bash, should I use declare instead of local and export?
  • Grouping command substitution variables into a subshell to run in background
  • How to display number of items in a folder - Bash
  • How to create a new procedure from bash script
  • Sed command to uppercase text between two specific strings
  • Escape double quotes in a Jenkins pipeline file's shell command
  • pgrep -f cuts commands after 4095 characters
  • I want my script to echo "$1" into a file literally
  • Shell script: unexpected token ´if"
  • scp error when defining a "PATH" variable in a bash script
  • Nested loop for reading two files with bash
  • Find folders with a specific name and run bash commands on each location
  • Replication and expansion of program flags in BASH script
  • Error on certain line: Syntax error: "(" unexpected
  • Terminal not returning to normal after Bash Script
  • what code function will help to ask for input when i try to execute a code with no previous value in shell script
  • “How to fix ‘Syntax Error Near’ error in BASH”
  • How to generate all ASCII characters with a brace expansion?
  • Why does `export` fail on bad substitutions but not command failures?
  • I am trying to get a public IP from an instance I am just starting up
  • How to run a bash script, but skip the last 2 lines?
  • Google Cloud Storage - Create many copies of a single object/file quickly
  • find all matches between two files then append character to beginning all matches in the second file
  • How to print 02-Jun-2019 in bash
  • added wrong path to .bash_profile now I can not found any command
  • How can I pick every line of a file with a match and do operations with it?
  • How to use awk and read together in bash script?
  • syntax error near unexpected token `(' when defining function in bash profile
  • How can I read a stream of key=value pairs in bash if the values can be empty?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com